Photo by Cyron - http://www.flickr.com/photos/cyron/3977863/
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 (11)

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
Post a comment
Home | Consulting | Articles | Blog | About | Contact
Copyright © 2008 Wheeler Software, LLC.