Autowire by Constructor in Spring | Spring Autowiring Example

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to learn about the Spring bean autowire by the constructor. In the previous tutorial, we had a discussion about bean autowire byType example.
Generally, if we want to inject the dependencies to a bean, we need to wire the dependencies explicitly by using the ref attribute as 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>
Autowire by the constructor is one of the strategies in spring autowiring. In this strategy, the spring container verifies the property type in bean and bean class in the XML file are matched or not. If both were matched then the injection will happen, otherwise, the property will not be injected.
Note: In the case of autowire by a constructor, the 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.
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
Spring Autowiring by Type
Spring autowiring
The complete example is available for download Spring-Autowiring-Constructor
Happy Learning 🙂


Spring-Autowiring-Constructor

File size: 10 KB
Downloads: 697


Spring Bean Autowire ByName Example
Spring Bean Autowire ByType Example
Types of Spring Bean Scopes Example
@Autowired,@Qualifier,@Value annotations in Spring
@Qualifier annotation example in Spring
Dependency Injection (IoC) in spring with Example
Spring Java Configuration Example
What is spring circular dependency problem ?
Spring Collection Map Dependency Example
Spring Collection Dependency List Example
What is Java Constructor and Types of Constructors
BeanFactory Example in Spring
Spring JdbcTemplate CRUD Application
Spring AOP Around Advice Example XML
spring expression language example

Spring Bean Autowire ByName Example
Spring Bean Autowire ByType Example
Types of Spring Bean Scopes Example
@Autowired,@Qualifier,@Value annotations in Spring
@Qualifier annotation example in Spring
Dependency Injection (IoC) in spring with Example
Spring Java Configuration Example
What is spring circular dependency problem ?
Spring Collection Map Dependency Example
Spring Collection Dependency List Example
What is Java Constructor and Types of Constructors
BeanFactory Example in Spring
Spring JdbcTemplate CRUD Application
Spring AOP Around Advice Example XML
spring expression language example