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.
  2. String and int, long…etc(primitive types).
  3. Collections(List,Set,Map…etc).

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.

Phone.java


package com.codegeekslab.device;

public interface Phone {

public void openApp(int number);

}

BasicPhone.java

&amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;gt;
package com.codegeekslab.type;

import com.codegeekslab.device.Phone;

public class BasicPhone implements Phone {
public void openApp(int number) {
System.out.println("calling via simcard... " + number);
}

}
&amp;amp;amp;amp;lt;pre&amp;amp;amp;amp;gt;

SmartPhone.java


package com.codegeekslab.type;

import com.codegeekslab.device.Phone;

public class SmartPhone implements Phone {
public void openApp(int number) {
System.out.println("calling via whatsapp..." + number);
}

}

CallingApp.java


package com.codegeekslab.app;

import com.codegeekslab.device.Phone;

public class CallingApp {

private Phone phone;
// for setter injection
public Phone getPhone() {
return phone;
}

public void setPhone(Phone phone) {
this.phone = phone;
}

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

}

beans.xml


&amp;amp;amp;amp;lt;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"&amp;amp;amp;amp;gt;

&amp;amp;amp;amp;lt;bean id="BasicPhone" class="com.codegeekslab.type.BasicPhone"/&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;bean id="SmartPhone" class="com.codegeekslab.type.SmartPhone"/&amp;amp;amp;amp;gt;

&amp;amp;amp;amp;lt;!-- &amp;amp;amp;amp;lt;bean id="CallingApp" class="com.codegeekslab.app.CallingApp"&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;constructor-arg ref="SmartPhone"/&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;constructor-arg ref="BasicPhone"/&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/bean&amp;amp;amp;amp;gt; --&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;bean id="CallingApp" class="com.codegeekslab.app.CallingApp"&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;property name="phone" ref="BasicPhone"&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/property&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/bean&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/beans&amp;amp;amp;amp;gt;

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.makeCall(777);

}
}

Output


Mar 14, 2017 4:07:09 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Mar 14, 2017 4:07:09 PM org.springframework.context.support.GenericXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericXmlApplicationContext@5e9f23b4: startup date [Tue Mar 14 16:07:09 IST 2017]; root of context hierarchy
calling via simcard... 777

In this example, we are setting the dependent object BasicPhone into the set method setPhone(Phone phone) of calling classusing <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.

Example:

All files remain same except CallingApp.java and beans.xml if we compare with our previous example of setter injection using dependent.

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

Mar 19, 2017 3:15:40 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Mar 19, 2017 3:15:41 PM org.springframework.context.support.GenericXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericXmlApplicationContext@5e9f23b4: startup date [Sun Mar 19 15:15:41 IST 2017]; root of context hierarchy
calling via whatsapp...777

In this example, we are setting the dependent object BasicPhone into the construtor  of calling class using   <constructor-arg ref=”SmartPhone”/> 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   <constructor-arg ref=”SmartPhone”/>,and output will change accordingly.

 

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.