Main Tutorials

Spring Autowiring by Name

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.mkyong.common.Customer" autowire="byName" />
	
	<bean id="address" class="com.mkyong.common.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.mkyong.common;
 
public class Customer 
{
	private Address address;
	//...
}

package com.mkyong.common;
 
public class Address 
{
	private String fulladdress;
	//...
}

2. Spring Wiring

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


	<bean id="customer" class="com.mkyong.common.Customer" >
		<property name="address" ref="address" />
	</bean>
	
	<bean id="address" class="com.mkyong.common.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.mkyong.common.Customer" autowire="byName" />
	
	<bean id="address" class="com.mkyong.common.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.mkyong.common.Customer" autowire="byName" />
	
	<bean id="addressABC" class="com.mkyong.common.Address" >
		<property name="fulladdress" value="Block A 888, CA" />
	</bean>

Output


Customer [address=null]

Download Source Code

Download it – Spring-AutoWiring-by-Name-Example.zip (6 KB)

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
10 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Ravella Baji
6 years ago

Thank you very much, sir…

Kunal Rana
8 years ago

best explanation of autowire..
thanks buddy

Babanna Duggani
8 years ago

autowired best explained…. thank u

Rishabh Kumar
1 year ago

this is the best explanation of autowire tha i have ever seen.

Sunil Yadu
3 years ago

Sir, could you please tell us Why or When should we use autowiring by name?

sumit
4 years ago

beautiful Explanation

Ben Salem Zied
9 years ago

good example

Guest
9 years ago

The App.javav class should be

package com.mkyong.common;

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”);

Address address = (Address) context.getBean(“address”);

System.out.println(“Address : “+ address.getFulladdress());

}

}

Dexter
11 years ago

Hi,
Nice Tut.
i have a doubt.
Should Customer class contain a setter method for Address? Is it must right? (when we are autowiring byName)

Renjith
10 years ago
Reply to  Dexter

I think we should have a setter method for address in Customer class, without it i am getting Customer [address=null] as output