Spring Beans AutoWiring byName Example

  • Facebook
  • Google
  • LinkedIn
  • Twitter

Container checks the spring configuration file and looks for the bean having name or id attribute same as the property name. This mode is applied on Setter methods. If there is more than one bean of same class with different bean name in configuration file, the autowiring will take the matching bean name with class property name.

Spring Bean Autowire "byName" Example


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



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;


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;



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

	<bean name="address" class="javatutsworld.Address">
		<property name="city" value="Saharanpur" />
		<property name="state" value="Uttar Pradesh" />
	<bean name="address2" class="javatutsworld.Address">
		<property name="city" value="Meerut" />
		<property name="state" value="Uttar Pradesh" />
	<bean name="student" class="javatutsworld.Student" autowire="byName">
		<property name="name" value="Deepak Kumar" />

In above configuration file, we have created two beans with name address and address2 of same class Address. But in Student class, the property name for Address class is address. So the autowiring in Student class will be performed using address bean.


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


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
Uttar Pradesh

For more details please see

Autowiring collaborators on http://docs.spring.io