del.icio.us Digg DZone Reddit StumbleUpon
Annotation-Based Autowiring in Spring 2.5 - Willie Wheeler
« Previous | 1 | 2 | 3 | Next »

Add annotations to name your DAO, service and controller classes

Chances are good that your DAO and service classes don't have the names that name-based autowiring would require. For example, you might define interfaces that have the "right" names, such as a CategoryDao interface, or a ProductService interface, and then your concrete implementation classes would be HibernateCategoryDao or ProductServiceImpl (or whatever), which won't autowire to the desired properties unless you have some strange and ill-advised property names. So our first order of business is to provide the requisite names. With manual wiring, you provide this on the <bean> element using the id or name attributes. But we're trying to eliminate said elements from our config, so that option is unavailable. We use annotations instead.

For each DAO, add a class-level @Repository annotation.

These annotations go on the implementation class, not on the interface.

import org.springframework.stereotype.Repository;
 
@Repository("productDao")
public final class ProductDaoImpl extends AbstractHibernateDao
  implements ProductDao {

    ...
}

The @Repository annotation works in Spring 2.0 as well.

For each service component, add a class-level @Service annotation.

As before, these go on the implementation class, not on the interface.

import org.springframework.stereotype.Service;
 
@Service("productService")
public final class ProductServiceImpl implements ProductService {
    ...
}

Note that @Service is new with Spring 2.5. So it won't work if you're using Spring 2.0 or earlier.

For each Spring MVC controller, add a class-level @Controller annotation.

Don't worry about providing a name; we won't need it. (So this step is a little out of place in these instructions, but go ahead and do it anyway as you will need the @Controller annotation if you want to fully autowire the web tier.) Just do this for each controller:

import org.springframework.stereotype.Controller;
 
@Controller
public final class ProductController {
    ...
}

Like the @Service annotation, @Controller is new with Spring 2.5.

At this point we've haven't really changed anything; you still have names defined in the manual configurations in addition to the annotation-based names you've just added. Test out your app to make sure we haven't broken anything, and then move on to the next step.

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

Comments (21)

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

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 in Practice
My brother and I are writing Spring in Practice for Manning!

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.