View RSS Feed

Spring Framework

How to Autowire a Spring Bean

Rate this Entry
by , 11-16-2011 at 04:23 PM (4526 Views)
In previous tips, we have shown how to wire bean’s properties using the <property> element. There is an alternative approach that can be used. It’s called autowiring. In this approach, Spring will wire your beans automatically if you set the autowire property on each <bean> on each of the beans that you want to autowire. There are four types of autowiring that Spring uses:

  • byName—Spring will attempt to find a bean in the container whose name (or ID) is the same as the name of the property being wired. If a matching bean is not found, then the property will remain unwired.
  • byType—Spring will attempt to find a single bean in the container whose type matches the type of the property being wired. If no matching bean is found, then the property will not be wired. If there is more than one bean matches the type the bean is looking for, an UnsatisfiedDependencyExcpetion will be thrown.
  • constructor—Spring tries to match up one or more beans in the container with the parameters of one of the constructors of the bean being wired. If either the beans or the constructors for the beans are ambiguous, then an UnsatisfiedDependencyException will be thrown.
  • autodetect—Spring attempts to autowire by constructor first and then using byType. If there a multiple beans matching the type or if the beans or the constructors for the beans are ambiguous, the same UnsatisfiedDependencyException will be thrown just as with byType or constructor type autowiring. In the example below look at the difference of when the carService is explicitly wired and is autowired.


For example, the declaration of the courseService bean when explicitly wired looks like this:

Java Code:
<bean id="carService" class="com.acme.springexamples.service.CarServiceImpl">
     <property name="carDao"> 
	<ref bean="carDao"/>
     </property> 
     <property name="accessoryService">
	<ref bean="accessoryService"/> 
     </property>
</bean>
Autowiring (in this example using byName), looks like this:

Java Code:
<bean id="carService" class="com.acme.springexamples.service.CarServiceImpl" 
autowire="byName"/>
As I noted above byName autowiring, Spring will attempt to find a bean in the container whose name is CarServiceImpl. Once it has found it, it will determine that there are two properties carDao and accessoryService, are eligible for autowiring through setter injection. If there are already beans declared in the wiring file with the names carDao and studentService, these beans will be used to wire carDao and accessoryService to carService. If we were doing byType autowiring, it would try to find the property type that matches with those identified for carService. Autowiring using byType works in a similar way to byName, except that instead of considering a property’s name, the property’s type is examined.

Autowiring by constructor with the accessoryService would look like the following:

Java Code:
<bean id="accessoryService" class="com.acme.springexamples.service.AccessoryServiceImpl" autowire="constructor"/>
The AccessoryServiceImpl class has a single-argument constructor that takes a AccessoryDao as an argument. If Spring’s container can find a bean whose type is com.acme.springexamples.dao.AccessoryDao, it will construct AccessoryServiceImpl by passing that bean to the constructor.

If we had created a setter for accessoryDao (i.e. setAccessoryDao()) then we could also do byType and byName autowiring. Finally if you want to take more or less a hands off attitude, you can let the container decide by using autodetect:

Java Code:
<bean id= "accessoryService" class="com.acme.springexamples.service.AccessoryServiceImpl" autowire="autodetect"/>
When you set autowire to autodetect, Spring container will attempt to autowire by constructor first. If it can’t find a suitable match between constructor arguments and beans, it will then try to autowire using byType. If it is not able to do either, it will throw an exception if it cannot sort out the ambiguities.

Submit "How to Autowire a Spring Bean" to Facebook Submit "How to Autowire a Spring Bean" to Digg Submit "How to Autowire a Spring Bean" to del.icio.us Submit "How to Autowire a Spring Bean" to StumbleUpon Submit "How to Autowire a Spring Bean" to Google

Comments