Spring Autowiring By Type

Autowiring using byType works in a similar way to byName, except that instead of con-sidering a property’s name, the property’s type is examined. When attempting to autowire a property by type, Spring will look for beans whose type is assignable to the property’s type.

You can configure this like :

<bean id="employee" class="com.kruders.bean.Employee" autowire="byType">
    <property name="name" value="Puneet" />
</bean>

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<dependencies>
 
    <dependency>
        <groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.8.2</version>
	<scope>test</scope>
    </dependency>

    <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring</artifactId>
	<version>2.5.6</version>
    </dependency>

</dependencies>

1. POJO

Now create Employee Class as following.

package com.kruders.bean;

public class Employee {
	private String name;
	private Address address;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
}

Now create Address Class as following

package com.kruders.bean;

public class Address {
	private String city;
	private String country;
	
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
}

2. Configuration

Create Spring-Bean.xml and write the following code

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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.xsd">

	<bean id="employee" class="com.kruders.bean.Employee" autowire="byType">
	   <property name="name" value="Puneet" />
	</bean>
		
	<bean id="address" class="com.kruders.bean.Address" >
		<property name="city" value="Delhi" />
		<property name="country" value="India" />
	</bean>

</beans>

But there’s a limitation to autowiring by type. What happens if Spring finds more than one bean whose type is assignable to the autowired property? In such a case, Spring isn’t going to guess which bean to autowire and will instead throw an exception.

To overcome ambiguities with autowiring by type, Spring offers two options: you can either identify a primary candidate for autowiring or you can eliminate beans from autowiring candidacy.

You can configure this like :

<bean id="address1" class="com.kruders.bean.Address" primary="true">
    <property name="city" value="Delhi" />
    <property name="country" value="India" />
</bean>
	
<bean id="address2" class="com.kruders.bean.Address" primary="false">
    <property name="city" value="Delhi" />
    <property name="country" value="India" />
</bean>

The primary attribute is only useful for identifying a preferred autowire candidate. If you’d rather eliminate some beans from consideration when autowiring, then you can set their autowire-candidate attribute to false, as follows:

<bean id="address1" class="com.kruders.bean.Address">
    <property name="city" value="Delhi" />
    <property name="country" value="India" />
</bean>
	
<bean id="address2" class="com.kruders.bean.Address" autowire-candidate="false">
    <property name="city" value="Delhi" />
    <property name="country" value="India" />
</bean>

3. Run Program

Create Main.java class that displays the employee and address details and run it as Java Application

package com.kruders.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.kruders.bean.Employee;

public class Main {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"Spring-Beans.xml");
		Employee employee = (Employee) context.getBean("employee");

		System.out.println("Name " + employee.getName());
		System.out.println("City " + employee.getAddress().getCity());
		System.out.println("State " + employee.getAddress().getCountry());
	}

}

When you run the above example you’ll get an output like:

Name Puneet
City Delhi
State India

The folder structure of the example is shown below in Figure 24.1

Figure 24.1 Figure 24.1

You can download the source code of this example here.


No comments yet.

Leave a Reply