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

pom.xml

<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">
	<modelVersion>4.0.0</modelVersion>
	<groupId>SpringExample</groupId>
	<artifactId>SpringExample</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.0.0.RELEASE</version>
		</dependency>
	</dependencies>

</project>

Address.java

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

Student.java

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

}

spring-config.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

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

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.

MainApp.java

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");
		System.out.println(student.getName());		
		System.out.println(student.getAddress().getCity());
		System.out.println(student.getAddress().getState());
        context.close();
    }
}

Output

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

For more details please see

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