Sunday, April 5, 2009

Dependencies injection with Spring annotations (@Repository, @Service, @Autowired)

One of the big downfall with frameworks relying on XML configurations file is that they are not synchronized with your code. For example, if you are doing a refactoring of your code like renaming a package or a class, you need to update your XML files too. If you're not, you will end up with a configuration exception at runtime.

Spring 2.5 introduces injection dependencies by annotation with stereotypes annotation like @Repository, @Service, etc... and the @Autowired annotation.

Here's how you define your beans the old school way :

applicationContext.xml :

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="beanDAO_1" class="com.yourcompany.dao.BeanDAO_1_Impl"/>
...
<bean id="beanDAO_N" class="com.yourcompany.dao.BeanDAO_N_Impl"/>

<bean id="beanServices_1" class="com.yourcompany.services.BeanServices_1_Impl">
<property name="beanDAO_1" ref="beanDAO_1"/>
</bean>
...
<bean id="beanServices_N" class="com.yourcompany.services.BeanServices_N_Impl">
<property name="beanDAO_N" ref="beanDAO_N"/>
</bean>

</beans>

BeanDAO_1_Impl.class :

package com.yourcompany.dao.impl;

public class BeanDAO_1_Impl implements BeanDAO_1 {
...
}

BeanServices_1.class :

package com.yourcompany.services.impl;

public class BeanServices_1_Impl implements BeanServices_1 {
private BeanDAO_1 beanDAO_1;
...
public setBeanDAO_1(BeanDAO_1 beanDAO_1) {
this.beanDAO_1 = beanDAO_1;
}
}


Here's how you define your beans with spring annotations :

applicationContext.xml :

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"/>

<context:component-scan base-package="com.yourcompany.dao.impl"/>
<context:component-scan base-package="com.yourcompany.services.impl"/>
</beans>

BeanDAO_1_Impl.class :

package com.yourcompany.dao.impl;

@Repository("beanDAO_1")
public class BeanDAO_1_Impl implements BeanDAO_1 {
...
}

BeanServices_1.class :

package com.yourcompany.services.impl;

@Service("beanServices_1")
public class BeanServices_1_Impl implements BeanServices_1 {
@Autowired
private BeanDAO_1 beanDAO_1;
//No need of a setter anymore. Spring can inject the beanDAO_1
//even if it's a private properties
}

And that's how it's done... No more big XML configuration files.

Consult the spring documentation for a detailed explanation of the differences between the annotation sterotypes (@Repository, @Services, @Component ...)

70 comments:

  1. Thank you men.. This is wht I want...

    ReplyDelete
  2. Fantastic! Thanks a lot.

    ReplyDelete
  3. Thanks a lot. Now I am clear...

    ReplyDelete
  4. Thanx, a simple no-nonsense example, this was what I needed! :)

    ReplyDelete
  5. I am still like the old school way!

    ReplyDelete
  6. Thank you very much Julien, very concise and quick to understand.

    ReplyDelete
  7. this is nice exactly what i wanted

    ReplyDelete
  8. Thank you very much man. How easily you showed it

    ReplyDelete
  9. AngularJS is a toolset for building the framework most suited to your application development. It is fully extensible and works well with other libraries. Every feature can be modified or replaced to suit your unique development workflow and feature needs. Read on to find out how.

    AngularJS Training in Chennai
    AngularJS Training Institute in Chennai

    AngularJS Certification Training in Chennai

    ReplyDelete
  10. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
    Devops Training in Chennai

    Devops Training in Bangalore

    Devops Training in pune

    Devops training in tambaram

    ReplyDelete
  11. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.
    python training institute in chennai
    python training in Bangalore
    python training in pune
    python online training

    ReplyDelete
  12. Woah this blog is wonderful i like studying your posts. Keep up the great work! You understand, lots of persons are hunting around for this info, you could help them greatly.

    Blueprism training in Chennai

    Blueprism training in Bangalore

    ReplyDelete
  13. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
    Data Science training in kalyan nagar | Data Science training in OMR
    Data Science training in chennai | Data science training in velachery
    Data science online training

    ReplyDelete
  14. You got an extremely helpful website I actually have been here reading for regarding an hour. I’m an initiate and your success is incredibly a lot of a concept on behalf of me.
    java training in chennai | java training in bangalore

    java online training | java training in pune

    ReplyDelete
  15. After seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs online Training

    angularjs Training in marathahalli

    angularjs interview questions and answers

    ReplyDelete
  16. Greetings. I know this is somewhat off-topic, but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform like yours, and I’m having difficulty finding one? Thanks a lot.

    AWS Interview Questions And Answers

    AWS Tutorial |Learn Amazon Web Services Tutorials |AWS Tutorial For Beginners


    AWS Online Training | Online AWS Certification Course - Gangboard

    AWS Training in Toronto| Amazon Web Services Training in Toronto, Canada

    ReplyDelete
  17. This is best one article so far I have read online, I would like to appreciate you for making it very simple and easy
    Regards,
    Devops Training in Chennai | Devops Certification in Chennai

    ReplyDelete
  18. This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.
    Devops Training in Chennai | Devops Training Institute in Chennai

    ReplyDelete
  19. I appreciate that you produced this wonderful article to help us get more knowledge about this topic. I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!

    Microsoft Azure online training
    Selenium online training
    Java online training
    Java Script online training
    Share Point online training

    ReplyDelete
  20. I likable the posts and offbeat format you've got here! I’d wish many thanks for sharing your expertise and also the time it took to post!!
    devops online training

    aws online training

    data science with python online training

    data science online training

    rpa online training

    ReplyDelete
  21. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.

    Best PHP Training Institute in Chennai|PHP Course in chennai
    Best .Net Training Institute in Chennai
    Big Data Hadoop Training in Chennai
    Linux Training in Chennai
    Cloud Computing Training in Chennai

    ReplyDelete
  22. keep up the good work. this is an Assam post. this to helpful, i have reading here all post. i am impressed. thank you. this is our digital marketing training center. This is an online certificate course
    digital marketing training in bangalore / https://www.excelr.com/digital-marketing-training-in-bangalore

    ReplyDelete
  23. Your work is very good and I appreciate you and hopping for some more informative posts
    Please check this ExcelR Courses in Digital Marketing in Pune

    ReplyDelete
  24. Data Science Courses
    It's really nice and meaningful. it's really cool blog. Linking is very useful thing. You have really helped lots of people who visit blog and provide them useful information.

    ReplyDelete
  25. thanks for posting this one....I found some useful information in your blog, it was awesome to read, thanks for sharing this

    BEST ANGULAR JS TRAINING IN CHENNAI WITH PLACEMENT

    https://www.acte.in/angular-js-training-in-chennai
    https://www.acte.in/angular-js-training-in-annanagar
    https://www.acte.in/angular-js-training-in-omr
    https://www.acte.in/angular-js-training-in-porur
    https://www.acte.in/angular-js-training-in-tambaram
    https://www.acte.in/angular-js-training-in-velachery

    ReplyDelete
  26. I would really like to read some personal experiences like the way, you've explained through the above article. I'm glad for your achievements and would probably like to see much more in the near future. Thanks for share.
    AWS training in chennai | AWS training in annanagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

    ReplyDelete
  27. The site was so nice, I found out about a lot of great things. I like the way you make your blog posts. Keep up the good work and may you gain success in the long run.


    Big Data Hadoop Training In Chennai | Big Data Hadoop Training In anna nagar | Big Data Hadoop Training In omr | Big Data Hadoop Training In porur | Big Data Hadoop Training In tambaram | Big Data Hadoop Training In velachery

    ReplyDelete
  28. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing...thanks
    Ai & Artificial Intelligence Course in Chennai
    PHP Training in Chennai
    Ethical Hacking Course in Chennai Blue Prism Training in Chennai
    UiPath Training in Chennai

    ReplyDelete
  29. I am looking for and I love to post a comment that "The content of your post is awesome" Great work!

    Simple Linear Regression

    Correlation vs Covariance

    ReplyDelete
  30. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    Correlation vs Covariance
    Simple linear regression
    data science interview questions

    ReplyDelete