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 to 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>
<bean id="carBean" class="Car">
  <property name="carName" value="BMW"></property>
</bean>

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 injects the dependencies.

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

 

To do the autowiring in spring, spring has given us autowire property in the <bean> tag. We have 4 different types of values associated with the autowire attribute. Those are autowire byName (our current example), 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. As we already discussed, the bean autowiring means dependencies are injected by spring container based on the strategy. In this strategy (autowire byName) 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.

Here we need to know one more important thing: what it is..?

The Question is Autowiring and injection are performed by the Spring container that’s cool, but we have different types of injections in spring. In this case which type of injection is performed by the spring container ? is it constructor or setter?

The answer is Setter injection.

In bean autowiring byName: Internally the injections are happening using setter injection type.

Bean Autowire byName Example :

Create a Car Bean :

[sourcecode language=”java”]

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

}

[/sourcecode]

Create a Travel Bean :

[sourcecode language=”java”]

public class Travel {
    String name;
    String type;
    private Car car;

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}
[/sourcecode]

Spring XML configuration :

spring.xml

[sourcecode language=”xml” highlight=”6″]
<?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-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <bean id="travelBean" class="Travel" autowire="byName">
        <property name="name" value="Agarwal Travels" />
        <property name="type" value="International" />
    </bean>
    <bean id="car" class="Car">
    <property name="carName" value="Audi"/>
    <property name="carType" value="Suv"/>
    </bean>
</beans>

[/sourcecode]

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 :

Main.java

[sourcecode language=”java”]

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

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);
    }
}
[/sourcecode]

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