Autowire Annotations
•        Spring 2.0 introduced configuration by annotation to the Spring Framework.
•        For example and as already shown, the @Required annotation can be applied to any bean property
setter method in the source code.
public class AccountServiceImpl implements AccountService {
AccountDao dao;

@Required
public void setDao (AccountDao dao) {
this.dao = dao;
}

public void processAccounts() {
...
Account[] accts = dao.getAccounts(nm[]);
...
}
}
•        At configuration time (in runtime), the property (the AccountDao in this case) must be set or a
BeanInitializationException is thrown.
•        Of interesting note, annotations such as this one are accomplished with the help of a
BeanPostProcessor in Spring.
•        Use of the BeanPostProcessor requires use of an application context (versus a bean factory) Spring
container.
•        Source level annotations, like this one, are one of the Spring 2.X features that require Java 5 (or better).

•        In Spring 2.5, a new autowire annotation can be used to autowire beans together by type without the
“autowire” attribute in the configuration file.
•        The @Autowired annotation provides more fine-grained control over where and how autowiring should
be accomplished.
•        The @Autowired annotation can be placed on the dependency injected class’s setter method as shown.
public class OrderServiceImpl implements OrderService {

private OrderDao orderDao;

@Autowired
public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
}
}
•        It can also be placed on the field.
public class OrderServiceImpl implements OrderService {

@Autowired
private OrderDao orderDao;

public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
}
}
•        Or it can be placed on the constructor.
public class OrderServiceImpl implements OrderService {

private OrderDao orderDao;

@Autowired        
public void OrderServiceImpl(OrderDao orderDao) {
this.orderDao = orderDao;
}
}

•        In fact, the @Autowired annotation can even be used with an arbitrary method.
public class OrderServiceImpl implements OrderService {

private OrderDao orderDao;

@Autowired        
public void getReady(OrderDao orderDao) {
this.orderDao = orderDao;
}
}
•        The annotation can even be used to collect all beans of a given type and inject them in an array or
collection.
public class OrderServiceImpl implements OrderService {

@Autowired
private OrderDao[] orderDaos;

}

public class OrderServiceImpl implements OrderService {

@Autowired
private List<OrderDao> orderDaos;

}
•        By default, the @Autowired annotation implies the dependency is required (like with the @Required
annotation above).
•        However, the default “required” injection behavior can be turned off by using “required=false” as
shown below.
@Autowired(required=false)
private OrderDao orderDao;

•        Autowiring, while convenient, can lead to issues of confusion or exceptions when the Spring container
is unable to resolve which bean to wire.
•        While there is a lot of flexibility in how autowiring is accomplished (by name, type, and constructor),
there is little control on how it is invoked.
•        This lack of fine-grained control has led some experienced Spring users to avoid the autowire feature
except in testing and prototyping situations.
•        However, the new @Autowire annotation also comes with additional annotations that allow more fine-
grained control on how it is applied.
•        The @Qualifier annotation is used with @Autowired to help explicitly pick which bean to inject when
autowiring by type leads to ambiguity.
•        Consider a bean configuration file where two OrderDao’s have been created.
<bean id="orderDao1" class="com.intertech.dao.OrderDaoImpl">
<property name="template">
<ref bean="hibernateTemplate"/>
</property>
</bean>

<bean id="orderDao2"  class="com.intertech.dao.OrderDaoImpl">
<property name="template">
<ref bean="JPATemplate"/>
</property>
</bean>        
•        Use of the @Autowired annotation here would lead to a runtime exception since no single bean can be
matched by type.
@Autowired
public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
}

No unique bean of type [com.intertech.dao.OrderDao] is defined: expected single matching bean but found 2:
[orderDao1, orderDao2]

•        To solve the issue and to provide fine-grained control over which bean should be dependency injected,
the @Qualifier can be used.
@Autowired
public void setOrderDao(@Qualifier("orderDao1") OrderDao orderDao) {
this.orderDao = orderDao;
}
•        The @Qualifier can be applied on a method or constructor parameter (as shown above) or on the field
(as shown below).
@Autowired
@Qualifier("orderDao1")
private OrderDao orderDao;
•        In this example, the bean name is used as the @Qualifier value.
•        In the bean configuration file, alternatively, a qualifier name can be provided and used by the @Qualifier
annotation.
<bean id="orderDao1" class="com.intertech.dao.OrderDaoImpl">
<qualifier value="mainDao"/>
<property name="template">
<ref bean="hibernateTemplate"/>
</property>
</bean>

@Autowired
@Qualifier("mainDao")
private OrderDao orderDao;
•        While there is not time to cover it here, you can create your own custom qualifier annotations to allow
for even more discretion when autowiring.

•        When using Spring annotations, the source code will also need the appropriate imports.  For those
shown here, the imports are below.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
•        The @Autowire and @Required annotations are accomplished in Spring with BeanPostProcessors.
•        Again, the BeanPostProcessor requires use of an application context.
•        In order to use the @Autowire annotation, a single Spring provided
AutowiredAnnotationBeanPostProcessor bean must be registered.
<bean class="org.springframework.beans. factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
•        A more concise means of doing this is by using the new Spring context namespace and the annotation-
config element.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.
org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.
springframework.org/schema/context/spring-context-2.5.xsd">

<context:annotation-config/>

</beans>
Autowire Annotations
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.