When writing a complex Java application, application classes should be as independent as possible of other Java classes to increase the possibility to reuse these classes and to test them independently of other classes while doing unit testing. Dependency Injection helps in gluing these classes together and same time keeping them independent
We can achieve more reusable code,more testable code,more readable code using dependency injection
Spring provides two ways of Dependency injection
1. Constructor based
Constructor-based DI is accomplished when the container invokes a class constructor with a number of arguments, each representing a dependency on other class.
2. Setter method
Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.
Here is one simple examples for both,
Use case: When buying a car they want to update the insurance details as well, basically when creating a Car Order object we need to add insurance as well..
Spring configuration details
Here we are creating the CarOrder bean based one setter injection and constructor injection as well.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="carInsurance" class="com.vinod.test.CarInsurance">
<property name="name" value="Progressive" />
<property name="policyDetails" value="Full cover" />
</bean>
<!-- Setter based dependency injection -->
<bean id="carOrder" class="com.vinod.test.CarOrder">
<property name="carInsurance" ref="carInsurance" />
</bean>
<!-- Constructor based dependency injection -->
<bean id="carOrder1" class="com.vinod.test.CarOrder">
<constructor-arg ref="carInsurance" />
</bean>
</beans>
CarInsurance.java
public class CarInsurance {
private String name;
private String policyDetails;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPolicyDetails() {
return policyDetails;
}
public void setPolicyDetails(String policyDetails) {
this.policyDetails = policyDetails;
}
@Override
public String toString() {
return "CarInsurance [name=" + name + ", policyDetails=" + policyDetails + "]";
}
}
CarOrder.java
public class CarOrder {
private String carDetails;
private CarInsurance carInsurance;
public String getCarDetails() {
return carDetails;
}
public void setCarDetails(String carDetails) {
this.carDetails = carDetails;
}
public CarInsurance getCarInsurance() {
return carInsurance;
}
public void setCarInsurance(CarInsurance carInsurance) {
this.carInsurance = carInsurance;
}
public CarOrder(CarInsurance carInsurance) {
super();
this.carInsurance = carInsurance;
}
public CarOrder() {}
@Override
public String toString() {
return "CarOrder [carDetails=" + carDetails + ", carInsurance=" + carInsurance + "]";
}
}
Test class
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDIExample {
public static void main(String[] args) {
// Test Setter based dependency injection
ApplicationContext context = new ClassPathXmlApplicationContext("spring-di-core.xml");
CarOrder cardOder = (CarOrder) context.getBean("carOrder");
System.out.println(cardOder);
// Test Constructor based dependency injection
ApplicationContext context1 = new ClassPathXmlApplicationContext("spring-di-core.xml");
CarOrder cardOder1 = (CarOrder) context1.getBean("carOrder1");
System.out.println(cardOder1);
}
}
Output
CarOrder [carDetails=null, carInsurance=CarInsurance [name=Progressive, policyDetails=Full cover]]
CarOrder [carDetails=null, carInsurance=CarInsurance [name=Progressive, policyDetails=Full cover]]
Download Example
No comments:
Post a Comment