Autowiring in Spring

As we know in Spring you can wire beans with their dependencies by doing XML configuration. This XML configuration works well for small scale application but
for big application to write so much XML is a hectic task. Fortunately Spring provides a few tricks to cut down on the amount of the XML configuration required.
And One of the way is Autowiring that I am going to discuss in this article.

When it comes to automatically wiring beans with their dependencies, Spring has lots of tricks to work from. As a result, Spring provides four flavors of autowiring:

byName

This byName autowiring tries to match all properties of the autowired bean with beans that have the same name or ID as the properties.
And the properties for which there is no matching bean will remain unwired.

Lets understand this by an example. Suppose there is a bean class named ‘EmployeeBean’ having two properties ‘name’ and ‘company’.
Comapny is just one another bean that we are using as properties in EmploeeBean class.

	package com.mindfire.bean;
 
	public class EmployeeBean {
     
		String name;
     
		//Assume Company is an another bean contains company information
	    Company company; 
		
		//Getter and Setter methods for name and company field here
	}
 

The traditional XML configuration for above bean will be like this:

    <!-- Declaring a bean with id 'employeeBean' and class value here. -->
	<bean id="employeeBean" class="com.mindfire.bean.EmployeeBean">
		<property name = "name" value = "Pranav" />
		<property name = "company" ref = "company" />
	</bean>
 
	<!-- Bean declaration for Company Bean class . -->
	<bean id = "company" class = "com.mindfire.bean.Company" />
  

now lets see how we will configure the same Employee bean using autowiring byName.

    <!-- Declaring a bean with id 'employeeBean' and class value here. -->
	<bean id="employeeBean" class="com.mindfire.bean.EmployeeBean" autowire="byName">
		<property name = "name" value = "Pranav" />
	</bean>
 
	<!-- Bean declaration for Company Bean class . -->
	<bean id = "company" class = "com.mindfire.bean.Company" />
  

So we see the difference that by using byName autowiring we don’t need to write this:
Spring automatically wires the ‘company’ bean declared in XML with EmployeeBean’s ‘company’ property when it finds the name of the bean declared is same
as property name in EmployeeBean.

byType

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.

Lets consider the same example we saw above with autowiring byName. In this case Spring continer will search for a bean whose type is ‘Company’ in XML configuration
and will wire that bean into the ‘company’ property of EmployeeBean class.

But there is a limitation to autowiring by type. What happens if Spring finds more than one <bean> declared whose type is assignable to the autowired property ?
In such case, Spring is not going to decide which bean to autowired and will just throw an exception.

To overcome ambiguities with autoworing by type, Spring offers two way:
– Either identify a primary candidate for autowiring or
– Eliminate bean from autowire candidancy.

To identify a primary autowire candidate, we need to work with element’s primary attribute. If only one autowire candidate has primary attribute
set to true, then that bean will be choosen over others. But the weird thing is that all bean’s primary is setted true by default.
That means that all autowire candidates will be primary. So, to use primary, you will need to set it to false for all beans that are not the primary choice.

You can apply this on any bean like this:

	<bean id = "company" class = "com.mindfire.bean.Company" primary="false" />
   

The primary attribute is only used to choose preferred candidate over others.

If you want to remove some beans in participating autowiring process, you can set their autowire-candidate attribute to false like this:

	<bean id = "company" class = "com.mindfire.bean.Company" autowire-candidate="false" />
   

constructor

If you do autowire = “constructor” for any bean, Spring tries to match up a constructor of the autowired bean with beans whose type are assignable to
the constructor arguments.

When we use constructor injection in spring, we use <construct-arg> element to pass required parameters to constructors.
The same thing can be acheived using autowire = “constructor”. In this case Spring automatically choose constructor arguments from beans in the Spring container.

Autowiring by constructor has same limitations as byType. Spring won’t attempt to guess which bean to autowire when it finds multiple beans that matches a constructor
arguments.

autodetect

This is the best fit autowiring. When you can’t decide which autowiring you should use, you can set autowire=”autodetect” and let Spring make decisions for you.
For a bean configured with autowire = “autodetect”, Spring tries to autowire by constructor first. If a suitable constructor match can’t be found, then Spring
will attempt to autowire by type.

Default Autowiring
You can also set default-autowire attribute for <beans> element in Container. Once you set this default-autowire for <beans> element, it will be
applied to all beans declared in that XML configuration file. You can do this like below:

    <?xml version="1.0" encoding="UTF-8"?>
	<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
	   default-autowire="byType">
 
    ....
 
	</beans>
  

In above configuration, default-autowire is setted to byType.
Since you have setted default-autowire attribute for , it doesn’t mean you are stuck with it for all beans.
You can still override the default on bean-by-bean basis using their autowire attribute.

Summary
We saw all four types of Autowiring method in Spring and also discussed the pros and cons of each. All has same purpose to reduce the amount of XML configuration.
As per our need we can decide which autowire suits best to our situation. There are also one other way called ‘Annotation’ to autowire beans in Spring
that I will discuss in my next blog.

Enjoy Spring 🙂

Pranav Kumar
Java/J2EE Developer, Mindfire Solutions
http://www.mindfiresolutions.com
Mindfire Solutions