Spring autowiring by type

In Previous article we have learned that Spring support different type of autowiring. One of them is byType.

Autowiring byType means whenever spring finds any property to be autowired, it will search for exactly one bean of given property type in container. If Spring find one(unique bean) it will autowire it. If it doesn’t find any, no auto wiring will be done(Property will not be set). If there are more than one bean of same type in container then Spring will throw Exception that byType can not be used here.

How to Enable byType Autowiring

byType Autowiring can be enabled by using autowire=”byType” like below.


<bean id="company" class="jbt.bean.Company" autowire="byType">
<property name="companyCEO" value="VN Gautam" />
</bean>

Here company bean has Address as property.

Spring Autowiring byType Example

Here we are using two beans Address and Company like below.

Address.java



package jbt.bean;

public class Address {

	@Override
	public String toString() {
		return "Address [streetNo=" + streetNo + ", houseNo=" + houseNo
				+ ", city=" + city + ", state=" + state + "]";
	}

	String streetNo;
	String houseNo;
	String city;
	String state;

	public String getStreetNo() {
		return streetNo;
	}

	public void setStreetNo(String streetNo) {
		this.streetNo = streetNo;
	}

	public String getHouseNo() {
		return houseNo;
	}

	public void setHouseNo(String houseNo) {
		this.houseNo = houseNo;
	}

	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;
	}

}

 

Company.java



package jbt.bean;

[email protected]
public class Company {

	@Override
	public String toString() {
		return "Company [companyName=" + companyName + ", companyCEO="
				+ companyCEO + ", address=" + address + "]";
	}

	String companyName;
	String companyCEO;

	[email protected]
	Address address;

	public Address getCompanyAddress() {
		return address;
	}

	public void setCompanyAddress(Address companyAddress) {
		this.address = companyAddress;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public String getCompanyCEO() {
		return companyCEO;
	}

	public void setCompanyCEO(String companyCEO) {
		this.companyCEO = companyCEO;
	}
}

 

Spring Bean Metadata

Here we are using XML to provide the metadata of Beans. Below is the content of XML.

JBTBean.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" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<context:component-scan base-package="jbt.bean" />

<bean id="address" class="jbt.bean.Address">
<property name="streetNo" value="2nd Street" />
<property name="houseNo" value="345" />
<property name="city" value="Mumbai" />
<property name="state" value="Maharastra" />
</bean>

<bean id="company" class="jbt.bean.Company" autowire="byType">
<property name="companyCEO" value="VN Gautam" />
</bean>

<!-- If you uncomment below bean definition you will get "No unique bean
of type [jbt.bean.Address] is defined" Exception by Spring. Because type
of below bean and above address bean is type even when id is different -->

<bean id="addressDuplicate" class="jbt.bean.Address"> 
   <property name="streetNo" value="2nd Street" /> 
   <property name="houseNo" value="345" /> 
   <property name="city" value="Mumbai" /> 
   <property name="state" value="Maharastra" /> 
</bean>

</beans>

Spring Autowiring

If you try to get the Company Bean by using below line.

        Company company = (Company) context.getBean("company");

 

        System.out.println(company.toString());

 

Output would be something like below

Company [companyName=null, companyCEO=VN Gautam, address=Address [streetNo=2nd Street, houseNo=345, city=Mumbai, state=Maharastra]]

 

If you uncomment other Address bean(addressDuplicate) in XML file you will get below exception.

org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [jbt.bean.Address] is defined: expected single matching bean but found 2: [address, addressDuplicate]

 

If you want to get source code of given article please send me a mail. With Subject line as “Spring autowiring by type”

Thanks

By | 2017-07-21T19:47:41+00:00 March 2nd, 2013|Spring|1 Comment

About the Author:

One Comment

  1. Jarvis April 26, 2017 at 11:18 am - Reply

    Spring autowiring by type

Leave A Comment