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.
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 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>
Bean Autowire by Constructor :
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.
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
References:
The complete example is available for download Spring-Autowiring-Constructor
Happy Learning 🙂