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.
@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.
@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.
@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.