SpringSpring Core

Types of Dependency Injection in Spring Framework

Types of Dependency Injection in spring framework: There are basically two types of dependency injection which are –

  1. Setter Injection.
  2. Constructor Injection.

There are generally three types of dependency which we inject:

  1. Dependent Object(Class object).
  2. String and int, long…etc(primitive types).
  3. Collections(List,Set,Map…etc).

Here the “types of dependency which we inject” are basically the types of argument which we pass to a parameter in a given method or a constructor.

Let’s understand all the above one by one with an example using setter and constructor injection.

Setter Injection using dependent object: In setter injection using a dependent object, we define a tag <property name=”name of reference” ref=”bean id”></property> in the XML file and a set method into which we want to inject our dependency.

Let’s understand above explanation by an example.

The below example is exactly same which we had seen in my last post on dependency injection in spring with few more details.

Download this application–>Download

CellPhone.java


package com.codegeekslab.device;

public interface CellPhone {

public void makeCall(int number);

}

BasicPhone.java


package com.codegeekslab.type;

import com.codegeekslab.device.CellPhone;

public class BasicPhone implements CellPhone {

public void makeCall(int number) {
System.out.println("calling via simcard... " + number);
}

}

SmartPhone.java


package com.codegeekslab.type;

import com.codegeekslab.device.CellPhone;

public class SmartPhone implements CellPhone {

public void makeCall(int number) {
System.out.println("calling via whatsapp..." + number);
}

}

CallingApp.java


package com.codegeekslab.app;

import com.codegeekslab.device.CellPhone;

public class CallingApp {

private CellPhone phone;

// for setter injection
public void setPhone(CellPhone phone) {
this.phone = phone;
}

public CellPhone getPhone() {
return phone;
}

public void dialNumber(int number) {
phone.makeCall(number);
}

}

beans.xml


<beans xmlns="http://www.springframework.org/schema/beans"
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="BasicPhone" class="com.codegeekslab.type.BasicPhone" />
<bean id="SmartPhone" class="com.codegeekslab.type.SmartPhone" />
<bean id="CallingApp" class="com.codegeekslab.app.CallingApp">
<property name="phone" ref="BasicPhone" />
</bean>

</beans>

Test.java


package com.codegeekslab.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

import com.codegeekslab.app.CallingApp;

public class Test {

public static void main(String[] args) {
ApplicationContext context = new GenericXmlApplicationContext("beans.xml");
CallingApp callingApp = (CallingApp) context.getBean("CallingApp");
callingApp.dialNumber(1419494494);
}
}

Output


Feb 28, 2017 12:44:04 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Feb 28, 2017 12:44:04 PM org.springframework.context.support.GenericXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericXmlApplicationContext@5e9f23b4: startup date [Tue Feb 28 12:44:04 IST 2017]; root of context hierarchy

calling via simcard... 1419494494

In this example, we are setting the dependent object BasicPhone into the set method setPhone(Phone phone) of calling class using <property name=”phone” ref=”BasicPhone”></property> tag in beans.xml.

We can also set the SmartPhone into the set method of calling class just by replacing the bean id BasicPhone into SmartPhone in the ref field of property tag like this <property name=”phone” ref=”SmartPhone”></property> ,and output will change accordingly.


Constructor Injection using dependent object: In constructor injection using a dependent object, we define a tag <constructor-arg=”name of reference” ref=”bean id”></constructor-arg> in the XML file and a set method into which we want to inject our dependency.

Let’s understand above explanation by an example.

All files remain same except CallingApp.java and beans.xml if we compare with our previous example of setter injection using dependent object.In this example instead of set method we will use constructor.

CallingApp.java


package com.codegeekslab.app;

import com.codegeekslab.device.Phone;

public class CallingApp {

private Phone phone;

//for constructor injection
public CallingApp(Phone phone) {
this.phone = phone;
}

public void makeCall(int number) {
phone.openApp(number);
}

}

beans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
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="BasicPhone" class="com.codegeekslab.type.BasicPhone"/>
<bean id="SmartPhone" class="com.codegeekslab.type.SmartPhone"/>

<bean id="CallingApp" class="com.codegeekslab.app.CallingApp">
<constructor-arg ref="SmartPhone"/>
</bean>

</beans>

Output

Dec 25, 2017 3:36:36 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Dec 25, 2017 3:36:36 PM org.springframework.context.support.GenericXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericXmlApplicationContext@378fd1ac: startup date [Mon Dec 25 15:36:36 IST 2017]; root of context hierarchy
calling via whatsapp...1419494494

In this example, we are setting the dependent object SmartPhone into the construtor  of calling class using   <constructor-arg ref=”SmartPhone”/> tag in beans.xml.

We can also set the BasicPhone into the constructor of CallingApp class just by replacing the bean id from SmartPhone to BasicPhone in the ref field of property tag like this <constructor-arg ref=”BasicPhone”/>,and output will change accordingly.

 

So In this tutorial we learnt about dependency injection of an object using setter and constructor injection, In our next tutorial, we will learn how to inject String, int and other primitive or wrapper classes using setter and constructor injection.