Spring Beans AutoWiring byType Example

  • Facebook
  • Google
  • LinkedIn
  • Twitter

Autowiring performs based on the class type of property and allows a property to be autowired if there is exactly one bean of the property type in the container. This mode is applied on Setter methods and if there are multiple beans found with the same class type in beans configuration then a fatal exception is thrown. But if there is no bean of required class type in the container then no autowiring will performed and also no error is thrown.

Spring Bean Autowire "byType" Example

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>SpringExample</groupId>
	<artifactId>SpringExample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>
	</dependencies>

</project>

Address.java

package javatutsworld;

public class Address {
	private String city;
	private String state;

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}
}

Student.java

package javatutsworld;

public class Student {
	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;
	}

}

spring-config.xml

<?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 name="address" class="javatutsworld.Address">
		<property name="city" value="Saharanpur" />
		<property name="state" value="Uttar Pradesh" />
	</bean>
	<bean name="student" class="javatutsworld.Student" autowire="byType">
		<property name="name" value="Deepak Kumar" />
	</bean>
</beans>  

In above configuration file, we have created one bean with name address for Address class. So using attribute autowire="byType". Autowiring will be performed without issue.

MainApp.java

package javatutsworld;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javatutsworld.Student;
public class MainApp {
	public static void main(String[] args) {
		AbstractApplicationContext  context = new ClassPathXmlApplicationContext("spring-config.xml");
		Student student=(Student)context.getBean("student");
		System.out.println(student.getName());		
		System.out.println(student.getAddress().getCity());
		System.out.println(student.getAddress().getState());
        context.close();
    }
}

Output

Mar 12, 2017 12:11:25 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6576fe71: startup date [Sun Mar 12 12:11:25 EDT 2017]; root of context hierarchy
Mar 12, 2017 12:11:25 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-config.xml]
Mar 12, 2017 12:11:25 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@6576fe71: startup date [Sun Mar 12 12:11:25 EDT 2017]; root of context hierarchy
Deepak Kumar
Saharanpur
Uttar Pradesh

But if there is more than one bean of same class type in the configuration file and using autowire="byType" then a error is thrown and autowiring will not be performed like below.

spring-config.xml

<?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 name="address" class="javatutsworld.Address">
		<property name="city" value="Saharanpur" />
		<property name="state" value="Uttar Pradesh" />
	</bean>
	<bean name="address2" class="javatutsworld.Address">
		<property name="city" value="Meerut" />
		<property name="state" value="Uttar Pradesh" />
	</bean>
	<bean name="student" class="javatutsworld.Student" autowire="byType">
		<property name="name" value="Deepak Kumar" />
	</bean>
</beans>  

Output

Mar 12, 2017 12:50:04 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6576fe71: startup date [Sun Mar 12 12:50:04 EDT 2017]; root of context hierarchy
Mar 12, 2017 12:50:04 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-config.xml]
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'student' defined in class path resource [spring-config.xml]: Unsatisfied dependency expressed through bean property 'address': : No qualifying bean of type [javatutsworld.Address] is defined: expected single matching bean but found 2: address,address2; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javatutsworld.Address] is defined: expected single matching bean but found 2: address,address2

For more details please see Autowiring collaborators on http://docs.spring.io

      Advertisements