Thursday , May 25 2017
Home / Spring / Spring Bean Autowire ByType Example

Spring Bean Autowire ByType Example

In this tutorial, we are going to learn about one of the autowire strategy 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 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=”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 super class, then we can pass all the sub classes to that super class type (runtime polymorphism). By using the Autowire byType, we can inject all the child classes to super class reference.

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 super class :


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.

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

}

Create Audi class which a subclass of Car class.

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 own the car.

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

<?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 Car. But we have two types of Cars in our example one is Benz and Audi, since Benz and Audi extends 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 a 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 :

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

The complete example is available for download.

Happy Learning 🙂

Download Example

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Check Also

Spring Bean Autowire ByName Example

In this tutorial, we are going to learn about Spring bean autowire byName. Wiring in …

One comment

  1. prashant.gole@gmail.com'

    Hello

    Can please provide me example of mvc with form in spring frame work 4 and above

Leave a Reply

Your email address will not be published. Required fields are marked *