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

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) { = car;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public Car getCar() {
        return car;

    public void setCar(Car 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=""
    xmlns:xsi="" xmlns:context=""
    <bean id="travelBean" class="Travel" autowire="constructor">
        <property name="name" value="Kesineni" />
    <bean id="car" class="Car">
        <property name="carName" value="BMW"></property>

Test the application :

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");
        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+ ,


Spring MVC Tiles Example (Apache Tiles)

In this tutorials, I am going to show you how to work with Spring MVC …

Leave a Reply

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