Spring bean autowire byName

In Spring framework, bean autowiring by name allows a property to be autowired such that it will inspect the container and look for a bean named exactly the same as the property which needs to be autowired.

For example, if you have a bean definition which is set to autowire by name, and it contains a “departmentBean” property (i.e. it has a setDepartmentBean(..) method), container will look for a bean definition named departmentBean, and if found, use it to set the property.

Read More : Spring bean autowiring modes

Autowiring byName Example

Bean definitions

A typical bean configuration file (e.g. applicationContext.xml) will look like this:

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

    <context:component-scan base-package="com.howtodoinjava" />    
    
   <bean id="employee" class="com.howtodoinjava.demo.beans.EmployeeBean" autowire="byName">
        <property name="fullName" value="Lokesh Gupta"/>
    </bean>
  
    <bean id="departmentBean" class="com.howtodoinjava.demo.beans.DepartmentBean" >
        <property name="name" value="Human Resource" />
    </bean>
    
</beans>

Autowire dependency using autowire=”byName”

In above configuration, I have enabled the autowiring by name for ’employee’ bean. It has been done using autowire="byName".

Let’s see the code.

package com.howtodoinjava.demo.beans;

public class EmployeeBean
{
    private String fullName;
     
    private DepartmentBean departmentBean;
 
    public DepartmentBean getDepartmentBean() {
        return departmentBean;
    }
 
    public void setDepartmentBean(DepartmentBean departmentBean) {
        this.departmentBean = departmentBean;
    }
 
    public String getFullName() {
        return fullName;
    }
 
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
}

And DepartmentBean looks like this which has been set:

package com.howtodoinjava.demo.beans;

public class DepartmentBean {
	private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Demo

To test that bean has been set properly, run following code:

package com.howtodoinjava.demo;

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

import com.howtodoinjava.demo.beans.EmployeeBean;

public class TestAutowire  {
	public static void main(String[] args) {

        ApplicationContext context =
                  new ClassPathXmlApplicationContext(new String[] {"application-context.xml"});
          
        EmployeeBean employee = (EmployeeBean) context.getBean ("employee");

        System.out.println(employee.getFullName());

        System.out.println(employee.getDepartmentBean().getName());
    }
}

Output:

Lokesh Gupta
Human Resource

Clearly, dependency was injected by name successfully.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

8 thoughts on “Spring bean autowire byName”

  1. Hi Lokesh, I get NullPointerException for line employee.getDeptBean().getDeptName() until I modify Employee Bean as:-

    It prints only Aniruddh Dwivedi i.e. fullName only.

    Reply
  2. i am new with spring and i am going through this example and i am getting BeanCreationException error, that string property cannot convert into the bean, no matching editors or conversion strategy found..

    Reply
  3. Just a little feedback. The above example does not wire “byName”…. the @autowire annotation implicitly wires by type. Test it by turning off the annotation and configure purely by xml using the autowire=”byName” attribute on the EmployeeBean.

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.