Thursday, 27 June 2013

Spring Dependency Injection (Constructor and Setter based) Example


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.


<beans xmlns=""

    <bean id="carInsurance" class="com.vinod.test.CarInsurance">
        <property name="name" value="Progressive" />
        <property name="policyDetails" value="Full cover" />
    <!-- Setter based dependency injection -->
    <bean id="carOrder" class="com.vinod.test.CarOrder">
            <property name="carInsurance" ref="carInsurance" />
    <!-- Constructor based dependency injection -->
    <bean id="carOrder1" class="com.vinod.test.CarOrder">
              <constructor-arg ref="carInsurance" />


package com.vinod.test;

public class CarInsurance {
    private String name;
    private String policyDetails;
    public String getName() {
        return name;
    public void setName(String name) { = name;
    public String getPolicyDetails() {
        return policyDetails;
    public void setPolicyDetails(String policyDetails) {
        this.policyDetails = policyDetails;
    public String toString() {
        return "CarInsurance [name=" + name + ", policyDetails=" + policyDetails + "]";


package com.vinod.test;

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) {
        this.carInsurance = carInsurance;
    public CarOrder() {}
    public String toString() {
        return "CarOrder [carDetails=" + carDetails + ", carInsurance=" + carInsurance + "]";

Test class

package com.vinod.test;

import org.springframework.context.ApplicationContext;

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

        // Test Constructor based dependency injection

        ApplicationContext context1 = new ClassPathXmlApplicationContext("spring-di-core.xml");

        CarOrder cardOder1 = (CarOrder) context1.getBean("carOrder1");



CarOrder [carDetails=null, carInsurance=CarInsurance [name=Progressive, policyDetails=Full cover]]

CarOrder [carDetails=null, carInsurance=CarInsurance [name=Progressive, policyDetails=Full cover]]


Download Example