Spring

INJECTION OF LIST (JAVA COLLECTION) USING SETTER AND CONSTRUCTOR DEPENDENCY INJECTION

In our previous tutorial we learned how to inject String, primitives, and objects using spring framework, In this tutorial, we will learn how to inject java collection java.util.List using Setter and Constructor Injection.

Setter Injection using List values: In setter injection using a List, we define a tag

<property name=”name of variable” >

<list>
<value>data 1</value>
<value>data 2</value>
<value>data n</value>
</list>

</property>

in the XML file and a set method into which we want to inject our dependency.

Example:

Consider I want to call list of numbers through my phone so I will implement the same example from our previous post but instead of passing a single number we will pass a list of numbers and we will call them one by one and we will inject other object and string as usual.

Phone.java


package com.codegeekslab.device;

import java.util.List;

public interface Phone {

public void openApp(List&amp;amp;amp;amp;amp;amp;amp;amp;lt;Integer&amp;amp;amp;amp;amp;amp;amp;amp;gt; number, String app);

}

 

BasicPhone.java


package com.codegeekslab.type;

import java.util.List;

import com.codegeekslab.device.Phone;

public class BasicPhone implements Phone {

public void openApp(List&amp;amp;amp;amp;amp;amp;amp;lt;Integer&amp;amp;amp;amp;amp;amp;amp;gt; number, String app) {
for(Integer num:number)
System.out.println("calling via " + app + " " + num);
}

}

SmartPhone.java


package com.codegeekslab.type;

import java.util.List;

import com.codegeekslab.device.Phone;

public class SmartPhone implements Phone {

public void openApp(List&amp;amp;amp;amp;amp;amp;amp;lt;Integer&amp;amp;amp;amp;amp;amp;amp;gt; number, String app) {
for(Integer num:number)
System.out.println("calling via " + app + " " + num);
}

}

CallingApp.java


package com.codegeekslab.app;

import java.util.List;

import com.codegeekslab.device.Phone;

public class CallingApp {

private Phone phone;

private List&amp;amp;amp;amp;amp;amp;amp;lt;Integer&amp;amp;amp;amp;amp;amp;amp;gt; number;

private String app;

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

public Phone getPhone() {
return phone;
}

public List&amp;amp;amp;amp;amp;amp;amp;lt;Integer&amp;amp;amp;amp;amp;amp;amp;gt; getNumber() {
return number;
}

public void setNumber(List&amp;amp;amp;amp;amp;amp;amp;lt;Integer&amp;amp;amp;amp;amp;amp;amp;gt; number) {
this.number = number;
}

public String getApp() {
return app;
}

public void setApp(String app) {
this.app = app;
}

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

}

beans.xml

&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;gt;

&amp;amp;lt;bean id="BasicPhone" class="com.codegeekslab.type.BasicPhone" /&amp;amp;gt;
&amp;amp;lt;bean id="SmartPhone" class="com.codegeekslab.type.SmartPhone" /&amp;amp;gt;
&amp;amp;lt;bean id="CallingApp" class="com.codegeekslab.app.CallingApp"&amp;amp;gt;
&amp;amp;lt;property name="phone" ref="SmartPhone"&amp;amp;gt;&amp;amp;lt;/property&amp;amp;gt;
&amp;amp;lt;property name="number"&amp;amp;gt;
&amp;amp;lt;list&amp;amp;gt;
&amp;amp;lt;value&amp;amp;gt;12345&amp;amp;lt;/value&amp;amp;gt;
&amp;amp;lt;value&amp;amp;gt;67899&amp;amp;lt;/value&amp;amp;gt;
&amp;amp;lt;value&amp;amp;gt;54321&amp;amp;lt;/value&amp;amp;gt;
&amp;amp;lt;/list&amp;amp;gt;
&amp;amp;lt;/property&amp;amp;gt;
&amp;amp;lt;property name="app" value="whatsapp"&amp;amp;gt;&amp;amp;lt;/property&amp;amp;gt;
&amp;amp;lt;/bean&amp;amp;gt;
&amp;amp;lt;/beans&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();

}
}

output

Mar 24, 2017 1:22:35 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Mar 24, 2017 1:22:35 PM org.springframework.context.support.GenericXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericXmlApplicationContext@5e9f23b4: startup date [Fri Mar 24 13:22:35 IST 2017]; root of context hierarchy
calling via whatsapp 12345
calling via whatsapp 67899
calling via whatsapp 54321

 

As you can see from the output “calling via whatsapp number” is getting called 3 times as we are passing the list of 3 numbers into the set method setNumber(List<Integer> number)  of CallingApp class through beans.xml.

In this example, we are injecting 3 items into all set methods of CallingApp class

  1. Dependent Object using <property name=”phone” ref=”SmartPhone”></property>.
  2. List of Integer values using <property name=”number”><list> <value>12345</value><value>67899</value> <value>54321</value> </list></property >.
  3. String Value using <property name=”app” value=”whatsapp”></property>

Constructor Injection using List values: In setter injection using a List, we define a tag

<constructor-arg>
<list>
<value>12345</value>
<value>67899</value>
<value>54321</value>
</list>
</constructor-arg>

in the XML file and a constructor with a parameter as list into which we want to inject our dependency.


Let’s implement the same above example using constructor injection.

All files will remain same except beans.xml and CallingApp.java

CallingApp.java


package com.codegeekslab.app;

import java.util.List;

import com.codegeekslab.device.Phone;

public class CallingApp {

private Phone phone;

private List&lt;Integer&gt; number;

private String app;

public CallingApp(Phone phone, List&lt;Integer&gt; number, String app) {
super();
this.phone = phone;
this.number = number;
this.app = app;
}

public void makeCall() {

phone.openApp(number, app);
}

}


beans.xml


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

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

&lt;bean id="CallingApp" class="com.codegeekslab.app.CallingApp"&gt;
&lt;constructor-arg ref="SmartPhone" /&gt;
&lt;constructor-arg&gt;
&lt;list&gt;
&lt;value&gt;12345&lt;/value&gt;
&lt;value&gt;67899&lt;/value&gt;
&lt;value&gt;54321&lt;/value&gt;
&lt;/list&gt;
&lt;/constructor-arg&gt;
&lt;constructor-arg value="whatsapp" /&gt;
&lt;/bean&gt;

&lt;/beans&gt;

output


Mar 28, 2017 10:59:42 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Mar 28, 2017 10:59:42 AM org.springframework.context.support.GenericXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericXmlApplicationContext@5e9f23b4: startup date [Tue Mar 28 10:59:42 IST 2017]; root of context hierarchy
calling 12345 via whatsapp
calling 67899 via whatsapp
calling 54321 via whatsapp


As you can see from the output “calling via whatsapp number” is getting called 3 times as we are passing the list of 3 numbers into the constructor public CallingApp(Phone phone, List<Integer> number, String app){ }  of CallingApp class through beans.xml.

In this example, we are injecting 3 items into all set methods of CallingApp class

  1. Dependent Object using <constructor-arg ref=”SmartPhone”>.
  2. List of Integer values using <constructor-arg><list><value>12345</value><value>67899</value><value>54321</value></list></constructor-arg>.
  3. String Value using <constructor-arg value=”whatsapp”></constructor-arg>

 

In our next tutorial, we will learn how to inject java collection Set using setter and constructor injection.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *