Cyron
Photo credit: Cyron
del.icio.us Digg DZone Reddit StumbleUpon
1 | 2 | 3 | Next »
Software Development

Annotation-Based Autowiring in Spring 2.5

Simplify Spring 2.5 configuration by autowiring with Java annotations.

This article gives a quick summary of how to do autowiring with Spring 2.5 using annotations. We cover the data and services tiers here, but not the web tier. For information on web-tier configuration, please see my article Annotation-Based MVC in Spring 2.5.

To set the proper context, our goal is to get rid of manual namings and wirings in the configuration files, and the basic strategy is convention-over-configuration: name the various dependencies according to the property names so that Spring can figure out how to autowire your app.

<aside>Incidentally, I take it to be a separate and interesting discussion as to whether autowiring is something that should be done in the first place. One might reasonably argue that wiring is a configuration issue, and that adding annotations everywhere amounts to distributing the configuration across the various classes, thereby violating the principle of separation of concerns. Not to mention the fact that we don't normally want to have to rebuild the app to make a config change. I can see that in some cases it makes sense to separate concerns (for example, I think modeling and persistence should generally be kept separate) and in other cases it doesn't (for example, code documentation should not be separated out). Anyway I won't argue here whether autowiring is good (I do think it is, but I won't argue that); I'll just explain how to do it.</aside>

Here I assume that you're using Spring MVC and Hibernate. I'm also assuming that you already have separate beans configuration files for the web, service, and data access tiers, that they're currently not autowired (i.e. you want to migrate from a manual wiring to an autowired configuration). Nothing essential hinges upon these assumptions—they just reflect my own configuration—and the instructions should be useful even if your configuration is somewhat different.

Here we go.

Social bookmarks: del.icio.us Digg DZone Reddit StumbleUpon
1 | 2 | 3 | Next »

Comments (25)

I enjoyed your article very much and I think this is very usefull information. I have a great fan of spring framework project and I use it wiht Struts 2 Web framework.

May question is.... would this @Controller annotations work in other Java web frameworks such as Struts 2?

Kind regards,

Flavio Oliva
By flavio Oliva on Mar 10, 2008 at 12:13 PM PDT
Hi Flavio. Thanks for your kind comments regarding the article. I'm also a big fan of Spring. :-) Regarding @Controller, that is a Spring-specific annotation. I haven't checked out Struts 2 yet so I don't know whether Struts 2 has a corresponding annotation, but I wouldn't expect Spring's @Controller annotation to work there.
By Willie Wheeler on Mar 10, 2008 at 7:59 PM PDT
Willie,

Nice article. I just wanted to point out that the "include-filter" in your component-scan element is not actually required since all of Spring's stereotype annotations (including: org.springframework.stereotype.Controller) are automatically included.

-Mark
By Mark Fisher on Mar 18, 2008 at 9:31 AM PDT
Hi Mark. Thanks for the comments. You are right about the <include-filter> part. In the app on which this code is based, I have the data, service and servlet configs separated out into different application context files, and I use the <include-filter> to avoid rescanning the same components over and over. But you are right that one could omit it and have a cleaner config. So take your pick. :-)
By Willie Wheeler on Mar 18, 2008 at 10:33 PM PDT
Hi Willie,

Say if I have this:

<bean id="hibernateDao" abstract="true">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>


<bean id="testDao" class="com.some.TestDaoImpl" parent="hibernateDao" />

public class TestDaoImpl extends HibernateDaoSupport implements TestDao {...

How do I convert my testDao to use annotation? I am clear on how to take care of the parent param.

Do you know if there are any spring configuration xml to annotation cheat sheets around? There are many more complex configurations in xml I don't see how to convert into annotations.

Thanks,

Derek
By Derek Lin on Jun 11, 2008 at 4:04 PM PDT
oops. I meant NOT clear:

Hi Willie,

Say if I have this:

<bean id="hibernateDao" abstract="true">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>


<bean id="testDao" class="com.some.TestDaoImpl" parent="hibernateDao" />

public class TestDaoImpl extends HibernateDaoSupport implements TestDao {...

How do I convert my testDao to use annotation? I am NOT clear on how to take care of the parent param.

Do you know if there are any spring configuration xml to annotation cheat sheets around? There are many more complex configurations in xml I don't see how to convert into annotations.

Thanks,

Derek
By Derek Lin on Jun 11, 2008 at 4:06 PM PDT
Hi Derek. I doubt there's a way to do that though I can't say with 100% certainty. If it's any consolation, though, you can autowire the session factory into the TestDao. I know that's not quite the same thing but it does reduce explicit XML configuration.
By Willie Wheeler on Jun 13, 2008 at 1:45 AM PDT
Magic! This has made my xml files so small I can hardly see them.

Thanks for another brilliantly written article, Willie.
By Paul Schwarz on Jun 17, 2008 at 8:39 AM PDT
Hi
Default spring will support autowire="no" and i read manning struts2 book struts2 it supprots default autowire to "byName" when it integrates with spring. Then which one it will consider. Either byName or no.
By Ramesh Alla on Aug 6, 2008 at 4:40 AM PDT
Hi Ramesh. Like I mentioned to Flavio above, I haven't used Struts 2 so I can't myself speak to that. (I understand from others though that Struts 2 is pretty good.) Maybe somebody else can answer this one?
By Willie Wheeler on Aug 6, 2008 at 8:03 PM PDT
Exellent
By Nick on Sep 4, 2008 at 8:01 PM PDT
I've found auto-wiring to be generally problematic and it often creates "interesting" bugs.

One of the things I love about Spring is the way it supports interface driven design. The major issue with auto-wiring is, it works best when there is only one implementation of an interface (an area where critics ask why bother using interfaces).

I often have multiple implementations of a given interface so, when auto-wiring does work (without throwing start-up exceptions) it often plugs in the wrong implementation.

OTOH, I've found auto-wiring works beautifully in tests. These are cases where small specific Spring config files are used. The simplicity offered by auto-wiring in unit tests is a wonderful thing.

Keep up the good work with the posts
By Paul on Oct 28, 2008 at 4:03 PM PDT
The article generally are nice on your website and this one is no exception, however i have one question related to transaction management, could that also be configured through annotations somehow?
By Vaibhav on Nov 19, 2008 at 3:55 PM PST
Hi Vaibhav. Yep. See

http://wheelersoftware.com/articles/spring-transactions-annotations.html
By Willie Wheeler on Nov 19, 2008 at 3:58 PM PST
Thanks sir!!! you rock!!
By Vaibhav on Nov 19, 2008 at 6:44 PM PST
I try. ;-)
By Willie Wheeler on Nov 19, 2008 at 7:47 PM PST

Very nice tutorial.

By Eswar on Jun 29, 2009 at 3:57 PM PDT

Newbie question :

Lets say my interface is CarService, and I've two implementing classes viz. FerrariService and HondaService with @Service("carService")

In my controller class I've a reference CarService carService. How will Spring determine which implementing class (Ferrari/Honda) am I looking for?

Do I have to change annotation in Service classes to @Service("ferrariService") and @Service("hondaService") respectively and in my controller use CarService ferrariService and CarService hondaService?

By AnnotationNewbie on Jul 1, 2009 at 7:05 AM PDT

Ok, I think I this post - http://sleeplessinslc.blogspot.com/2008/02/introduction-software-development-and.html explains it.

By AnnotationNewbie on Jul 10, 2009 at 10:12 AM PDT

really very nice tutorial. I like this . The steps followed by you is really great.

Thanks a lot.

Regards, Sunil

By sunil kumar on Sep 29, 2009 at 11:14 PM PDT

Hi:

Really noice tutorial!! Is there a link to download the source code to try out?

Yours,

John

By John Smith on Dec 30, 2009 at 11:54 PM PST

is there something we can do in the meantime? seems this affects autowiring across the board. is the change minor enough that i can simply compile my own version and use that as a work around until such time that the versions that you mention are released?

By fico score on Aug 29, 2010 at 10:43 AM PDT

he myth of pandora is ancient, appears in several distinct Greek versions, pandora armbandand has been interpreted in many ways. In all literary versions, Neu Eingetroffen however, Pandora Armbänder the myth is a rosetta stone kind of theodicy, addressing the question pop information, web easy get, sports fashion, news-fashionof why there is evil in the world. In the seventh hot-winter century BC, Hesiod, both in his Theogony (briefly, without naming Pandora outright rosetta stone language, rosetta stone spanish, abercrombie and fitch, Abercrombie Fitch

By pandora schmuck on Aug 30, 2010 at 11:08 PM PDT

he myth of pandora is ancient, appears in several distinct Greek versions, pandora armbandand has been interpreted in many ways. In all literary versions, Neu Eingetroffen however, Pandora Armbänder the myth is a rosetta stone kind of theodicy, addressing the question pop information, web easy get, sports fashion, news-fashionof why there is evil in the world. In the seventh hot-winter century BC, Hesiod, both in his Theogony (briefly, without naming Pandora outright rosetta stone language, rosetta stone spanish, abercrombie and fitch, Abercrombie Fitch

By pandora schmuck on Aug 30, 2010 at 11:08 PM PDT

Post a comment

Your name:
Your e-mail address (won't be displayed):
Your web site (optional):
example: www.xyz.com
Your comment:
Preview:
By You
Please help us reduce comment spam:
Spring Annotations RefCard
Check out the new DZone Spring Annotations Refcard by Craig Walls!

What's New?

2009-08-30 - Check out my two-part series on DZone: Spring Integration: A Hands-On Tutorial.
2009-03-25 - My new article Getting Started with Spring Batch 2.0 is available on DZone.
Home | Consulting | Tech Articles | Mailing List | Contact | Spring Blog
Copyright © 2008 Wheeler Software, LLC.