Concept to auto-wire relationships among spring enabled beans has always been there. The idea with auto-wiring is to get away from the tedious task of specifying and more importantly maintaining explicit wiring. Originally it was supported to be done by name, by type, by constructors or auto-detect and then you had the option to auto-wire all or specific beans within a context. But now with Spring 2.5 the auto wiring concept has taken a whole new meaning and so is the debate if we really want to do auto-wiring.
Spring 2.5 has a new @Autowire annotation. @Autowire annotation let us do much fine grained auto-wiring then was possible before and also it make us much more explicit then was possible in pre Spring 2.5 times
Lets consider few examples
1
[java]@Autowired
public void init(AccountDao accountDao, CustomerDao customerDao) {
this.accountDao = accountDao;
this.customerDao = customerDao;
}[/java]
Do not need to have setters with one parameter to inject dependencies, any method with any name and any number of parameters will do. Can be applied to fields and constructors as well and obviously to the favorite setters 🙂
2
[java]@Autowired
private BaseDao[] daos;[/java]
or
[java]@Autowired
private Set daos;[/java]
Create an array or collection with all the possible beans available in the context..
3
[java]@Autowired(required=false)
private AccountDao accountDao = new AccountDao();[/java]
Find it do it, if not leave it.
Moving on we can even fine control this by using another annotation @Qualifier. Again lets see some more examples
4
[java]@Autowired
@Qualifier("bankService")
private BankService bkService;[/java]
We are doing by-name auto-wiring by providing a bean name within the annotation. This might help by letting you declare the name of the property different from the name of the bean.
5 – Example of custom qualifier annotations to take care of the case when we have more then one implementation which we want to auto-wire
Define an annotation like this
[java]@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Category {
String value();
}[/java]
Custom qualifier on autowired fields can be applied like this
[java]public class BookList {
@Autowired
@Category("Technical")
private Book technicalBook;
@Autowired
@Category("Management")
private Book managementBook;
}[/java]
and then do the bean definitions like this
[xml]
[/xml]
And if this is not enough we can always create a custom auto-wire configurer or may be use the new @Resource annotation
for further auto-wiring stuff.
That is that for auto-wiring but Spring 2.5 introduces a concept of auto-detection of spring components on the classpath where we do not have to even define the beans (with the name and bean class) in the context. Spring does a component scanning on the classpath (which can be configured using filters) by detecting classes which are stereotyped and then registering them as spring enabled beans.
Yes it provides a lot of flexibility and a much greater control as can be seen from all of the above examples but still I feel it should be done with a lot of caution and I-am-going-to-be-consistent attitude.