Spring autowiring by name example

In Previous article we have learned about autowiring byType. In this article we will learn about the Autowiring by Name.

Autowiring byName means whenever spring finds any property to be autowired, it will search for exactly one bean of given property name 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 byName can not be used here.

How to Enable byType Autowiring

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

 



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

Here company bean has Address as property.

Spring Autowiring byName Example

In this example we will use two beans named Address and Company.

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;

import org.springframework.stereotype.Component;

@Component
public class Company {

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 * 
	 * Method is Overriden to provide proper output while using in sysout.
	 */
	@Override
	public String toString() {
		return "Company [companyName=" + companyName + ", companyCEO="
				+ companyCEO + ", address=" + address + "]";
	}

	String companyName;
	String companyCEO;

	/*
	 * This property will be Autowired by Name. Spring will search for any bean
	 * with name as "address".
	 */
	Address address;

	/*
	 * Note: Setter and Getter method should be in the form of bean rules. If
	 * you write setter & getter methods in other format Autowire will not work
	 * as expected.
	 */
	public Address getAddress() {
		return address;
	}

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

	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

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">

<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<!-- Type (Class) of below beans are same still it will not create any problem
while autowiring as "byName" autowiring is used here instead of "byType"
and id is unique for both the bean. -->
<bean id="addressDuplicate" class="jbt.bean.Address">
<property name="streetNo" value="3rd Street" />
<property name="houseNo" value="9393939" />
<property name="city" value="New Delhi" />
<property name="state" value="Karnatak" />
</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>

<!-- Note: If you specify the property in a Bean you have to provide either
ref or value otherwise Spring will throw an Exception like Below
"Configuration problem: <property> element for property 'address' must specify a ref or value"
-->

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

</beans>
	

Spring Autowiring

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

  Company company = (Company) context.getBean("company");
  System.<em>out</em>.println(company.toString());

Output would be something like below



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

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

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.