Spring Autowiring By Name

In Spring, everything is given a name. Thus bean properties are given names. This option will inspect the container and look for a bean named exactly the same as the property which needs to be autowired.

You can configure this like :

<bean id="employee" class="com.kruders.bean.Employee" autowire="byName">
	<property name="name" value="Puneet" />

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml





Now create Employee Class as following.

package com.kruders.bean;

public class Employee {
	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;

Now create Address Class as following

package com.kruders.bean;

public class Address {
	private String city;
	private String country;
	public String getCity() {
		return city;
	public void setCity(String city) {
		this.city = city;
	public String getCountry() {
		return country;
	public void setCountry(String country) {
		this.country = country;

2. Configuration

Create Spring-Bean.xml and write the following code

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

	<bean id="employee" class="com.kruders.bean.Employee" autowire="byName">
	   <property name="name" value="Puneet" />
	<bean id="address" class="com.kruders.bean.Address" >
		<property name="city" value="Delhi" />
		<property name="country" value="India" />


There is no need to declare the property tag for address in employee, as “address” bean is same as one of the property of “employee” bean.

3. Run Program

Create Main.java class that displays the employee and address details and run it as Java Application

package com.kruders.core;

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

import com.kruders.bean.Employee;

public class Main {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
		Employee employee = (Employee) context.getBean("employee");

		System.out.println("Name " + employee.getName());
		System.out.println("City " + employee.getAddress().getCity());
		System.out.println("State " + employee.getAddress().getCountry());


When you run the above example you’ll get an output like:

Name Puneet
City Delhi
State India

If you change the “address” bean like:

<bean id="addressABC" class="com.kruders.bean.Address" >
    <property name="city" value="Delhi" />
    <property name="country" value="India" />

Null is assigned to the property, as Spring does not find bean with same name as that of property.

The folder structure of the example is shown below in Figure 22.1

Figure 22.1 Figure 22.1

You can download the source code of this example here.

No comments yet.

Leave a Reply