Monday , February 27 2017
Home / Spring / Spring Bean Autowire By Constructor Example

Spring Bean Autowire By Constructor Example

In this tutorial, we are going to learn about Spring bean autowire by constructor. In the previous tutorial we had a discussion about bean autowire byType example.

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

Bean Autowire by Constructor :

Autowire by constructor is one of the strategy in spring autowiring. In this strategy spring container verifies the property type in bean and bean class in xml file are matched or not. If both were matched then the injection will be happen, otherwise property will not be injected.

In the case of autowire by constructor, Spring container injects the dependencies by using the constructor injection.

The only difference between autowire byType and constructor is : byType injects the dependencies by using setter injection and constructor injects the dependencies by using constructor injection.

Bean Autowire by Constructor Example :

Create a Travel Bean :


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

    public Travel(Car car) {
        this.car = car;
    }

    public String getName() {
        return name;
    }

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

    public Car getCar() {
        return car;
    }

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

}

Create a Car Bean :


public class Car {
    private String carName;

    public String getCarName() {
        return carName;
    }

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

}

Create spring configuration :


<?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="constructor">
        <property name="name" value="Kesineni" />
    </bean>
    <bean id="car" class="Car">
        <property name="carName" value="BMW"></property>
    </bean>
</beans>

Test the application :


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");
        System.out.println("Travel Name: "+travel.getName());
        System.out.println("Car Name : "+travel.getCar().getCarName());    
    }
}

Output :

Travel Name: Kesineni
Car Name : BMW

The complete example is available for download Spring-Autowiring-Constructor

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 AOP ThrowsAdvice Example XML

In this tutorial, we are going to learn about Spring AOP ThrowsAdvice. In the previous …

Leave a Reply

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