In this tutorial, we are going to learn about one of the autowire strategies in Spring, bean autowire byType.  In the previous example, we had a long discussion about autowire byName. If you are not coming through the previous, It is recommended to go through autowire byName.

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

spring.xml
<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 inject the dependencies.

spring.xml
<bean id="travelBean" class="Travel" autowire="byType">
</bean>
<bean id="carBean" class="Car">
  <property name="carName" value="BMW"></property>
</bean>

Bean Autowire ByType :

In this strategy, the spring container verifies a property type and a bean class in XML are matched or not. If matched then the property will be injected automatically. If not the property will not be injected.

Here type means, the variable type. It might be an Integer, String or Employee. The basic advantage of this approach is, we can implement the inheritance strategies in our application.

That means if we define a superclass, then we can pass all the subclasses to that superclass type (runtime polymorphism). By using the Autowire byType, we can inject all the child classes to superclass reference.

Note: In the case of autowire byType, the Spring container injects the dependencies by using the setter injection method.

Bean Autowire ByType Example :

Here is the complete example for spring autowire byType.

Create Car superclass :

Car.java
package com.onlinetutorialspint.service;

public class Car {
    private int carId;
    private String carName;
    private String cc;

    public int getCarId() {
        return carId;
    }

    public void setCarId(int carId) {
        this.carId = carId;
    }

    public String getCarName() {
        return carName;
    }

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

    public String getCc() {
        return cc;
    }

    public void setCc(String cc) {
        this.cc = cc;
    }

}

Create Benz class, which is a subclass of Car.

Benz.java
package com.onlinetutorialspint.impl;
import com.onlinetutorialspint.service.Car;
public class Benz extends Car {

}

Create Audi class which a subclass of Car class.

Audi.java
package com.onlinetutorialspint.impl;
import com.onlinetutorialspint.service.Car;
public class Audi extends Car {

}

To make the example as simple, we didn’t write much functionality in Audi and Benz classes. Which inherited the methods from Car class.

Create a Person class, who has owned the car.

Person.java
import com.onlinetutorialspint.service.Car;
public class Person {
    private int personId;
    private String personName;
    private Car car;

    public int getPersonId() {
        return personId;
    }

    public void setPersonId(int personId) {
        this.personId = personId;
    }

    public String getPersonName() {
        return personName;
    }

    public void setPersonName(String personName) {
        this.personName = personName;
    }

    public Car getCar() {
        return car;
    }

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

}

spring configuration file :

spring.xml

spring.xml
<?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="person" class="Person" autowire="byType">
        <property name="personId" value="1001" />
        <property name="personName" value="Chandra Shekhar" />
    </bean>

    <bean id="audi" class="com.onlinetutorialspint.impl.Audi" >
        <property name="carId" value="200" />
        <property name="carName" value="Audi" />
        <property name="cc" value="1500" />
    </bean>
</beans>

On the above XML configuration : we have defined the person <bean> as autowire=”byType”. In the Person bean, we have a property name called “car” with the type CarBut we have two types of Cars in our example one is Benz and Audi since Benz and Audi extend from Car class. So that we can inject Benz or Audi into Car type.

In the XML file, we have created Audi <bean>, which is the type of Car. So that After creating the Person class object, spring container itself, will inject the available car type bean (Audi) into person object using setter injection.

In the above XML, we can not define the two Car types at a time i.e. we can not define Benz and Audi beans at the same time. Because at runtime Spring will throw an exception like NoUniqueBeanDefinitionException, Since two beans (Benz and Audi) are available to inject with the type of Car, we can say it as ambiguity. To resolve this exception, we need to use qualifier in Spring.

Output :

Terminal
Person Id : 1001
Person Name : Chandra Shekhar
Car Name : Audi

The complete example is available for download.

Happy Learning 🙂

Download Example