In this tutorial, we are going to learn about Spring bean autowire byName. Wiring in spring is nothing but configuring the dependencies of a bean in the XML file.

What is Autowiring :

Generally, if we want to inject the dependencies into a bean, we need to wire the dependencies explicitly by using the ref attribute. like below

<bean id="travelBean" class="Travel">
  <property name="car" ref="carBean" />
<bean id="carBean" class="Car">
  <property name="carName" value="BMW"></property>

By making use of the autowire mechanism in spring, we no need to wire the dependencies explicitly. Just by using the autowire strategies, spring will automatically inject the dependencies.

Spring has given an autowire property in the <bean> tag to wire the dependencies. We have 4 different types of values associated with the autowire attribute. Those are autowire byName, byType, constructor, and auto-detect.

Let’s understand the autowire byName.

Bean Autowire byName :

In spring, autowire byName is one of the strategies in bean autowiring strategies. In this strategy, while injecting the properties spring container verifies whether a property name of bean class and bean id in XML are matched or not. If matched then that property will be injected.

Spring IOC always uses the setter injection in case of autowire byName.

Bean Autowire byName Example :

Create a Car Bean :
public class Car {
    private String carName;
    private String carType;

    public String getCarName() {
        return carName;

    public void setCarName(String carName) {
        this.carName = carName;

    public String getCarType() {
        return carType;

    public void setCarType(String carType) {
        this.carType = carType;


Create a Travel Bean :
public class Travel {
    String name;
    String type;
    private Car car;

    public Car getCar() {
        return car;

    public void setCar(Car car) { = car;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getType() {
        return type;

    public void setType(String type) {
        this.type = type;

Spring XML configuration :


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    xmlns:xsi="" xmlns:context=""
    <bean id="travelBean" class="Travel" autowire="byName">
        <property name="name" value="Agarwal Travels" />
        <property name="type" value="International" />
    <bean id="car" class="Car">
    <property name="carName" value="Audi"/>
    <property name="carType" value="Suv"/>

On the above XML in line number 6, we declare a travel bean with autowire="byName" and we inject name and type property values. But we didn’t inject any property for car bean in travel <bean>.

But, since it is declared as autowire byName, the container checks whether the property name car is mating with any bean id in XML, if it matches, that bean will be injected automatically. In this case, the car is injected to Travel bean, since the property name in Travel class car is matched with bean id in the car <bean> in XML.

If we remove the autowire="byName" attribute from travel <bean>. We should get NullPointerException since it is not defined.

Run the Application :
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

public final class Main {

    public static final void main(String[] args) {
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("spring.xml"));
        Travel travel = (Travel)factory.getBean("travelBean");
        String carName = travel.getCar().getCarName();

        System.out.println("Travel Name : "+travel.getName());
        System.out.println("Travel Type : "+travel.getType());
        System.out.println("Car Name : "+carName);

Output :

Travel Name : Agarwal Travels
Travel Type : International
Car Name : Audi

Complete Example is available for download : Spring Bean Autowire byName Example

Happy Learning 🙂

Download Example