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 »
Show comments (16)

Post a comment

Your name:
Your e-mail address (won't be displayed):
Your web site (optional):
example: www.xyz.com
Your comment:
Please help us prevent comment spam:

What's New?

2008-10-20 - I've added a new mailing list feature to the site. Sign up to receive e-mail updates about new articles.
2008-09-30 - We've released chapter 4 (User registration) and chapter 5 (Authentication) of Spring in Practice.
2008-09-11 - By popular demand, I've added an RSS feed to the site.
Home | Consulting | Tech Articles | Mailing List | About | Contact | Spring Blog
Copyright © 2008 Wheeler Software, LLC.