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

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


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;



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) { = 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 :


[sourcecode language=”xml” highlight=”6″]
<?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 :

[sourcecode language=”java”]

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