In Spring, “Autowiring by Name” means, if the name of a bean is same as the name of other bean property, auto wire it.

For example, if a “customer” bean exposes an “address” property, Spring will find the “address” bean in current container and wire it automatically. And if no matching found, just do nothing.

You can enable this feature via autowire="byName" like below :

	<!-- customer has a property name "address" -->
	<bean id="customer" class="com.tuts.heomi.netmon.Customer" autowire="byName" />
	
	<bean id="address" class="com.tuts.heomi.netmon.Address" >
		<property name="fulladdress" value="Block A 888, CA" />
	</bean>

See a full example of Spring auto wiring by name.

1. Beans

Two beans, customer and address.

package com.tuts.heomi.netmon;

public class Customer 
{
	private Address address;

	public Address getAddress() {
		return address;
	}

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

	@Override
	public String toString() {
		return "Customer [address=" + address + "]";
	}

}
package com.tuts.heomi.netmon;

public class Address 
{
	private String fulladdress;

	public String getFulladdress() {
		return fulladdress;
	}

	public void setFulladdress(String fulladdress) {
		this.fulladdress = fulladdress;
	}

	@Override
	public String toString() {
		return "Address [fulladdress=" + fulladdress + "]";
	}
	
}

2. Spring Wiring

Normally, you wire the bean explicitly, via ref attribute like this :

	<bean id="customer" class="com.tuts.heomi.netmon.Customer" >
		<property name="address" ref="address" />
	</bean>
	
	<bean id="address" class="com.tuts.heomi.netmon.Address" >
		<property name="fulladdress" value="Block A 888, CA" />
	</bean>

Output

Customer [address=Address [fulladdress=Block A 888, CA]]

With autowire by name enabled, you do not need to declares the property tag anymore. As long as the “address” bean is same name as the property of “customer” bean, which is “address”, Spring will wire it automatically.

	<bean id="customer" class="com.tuts.heomi.netmon.Customer" autowire="byName" />
	
	<bean id="address" class="com.tuts.heomi.netmon.Address" >
		<property name="fulladdress" value="Block A 888, CA" />
	</bean>

Output

Customer [address=Address [fulladdress=Block A 888, CA]]

See another example, this time, the wiring will failed, caused the bean “addressABC” is not match the property name of bean “customer”.

	<bean id="customer" class="com.tuts.heomi.netmon.Customer" autowire="byName" />
	
	<bean id="addressABC" class="com.tuts.heomi.netmon.Address" >
		<property name="fulladdress" value="Block A 888, CA" />
	</bean>

Output

Customer [address=null]

3. Testing

package com.tuts.heomi.netmon;

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

public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
        Customer cust = (Customer) context.getBean("customer");
        System.out.println(cust);
    }
}

Download Source Code

$ git clone https://github.com/favtuts/java-spring-tutorials.git
$ cd SpringAutoWiringByName

Reference Articles

Leave a Reply

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