Spring

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

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

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

<property name=”name of variable” >

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

</property>

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

Example:

As in our previous tutorial of “injection of List” we passed a list of objects, In this example, we will pass a list of Set and as you know Set doesn’t contain duplicate items, so if we try to pass two duplicate items it will consider it as a single item only.

Phone.java


package com.codegeekslab.device;

import java.util.Set;

public interface Phone {

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

}


SmartPhone.java


package com.codegeekslab.type;

import java.util.Set;

import com.codegeekslab.device.Phone;

public class SmartPhone implements Phone {

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

}


BasicPhone.java


package com.codegeekslab.type;

import java.util.Set;

import com.codegeekslab.device.Phone;

public class BasicPhone implements Phone {

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

}


CallingApp.java


package com.codegeekslab.app;

import java.util.Set;

import com.codegeekslab.device.Phone;

public class CallingApp {

private Phone phone;

private Set&lt;Integer&gt; number;

private String app;

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

public Phone getPhone() {
return phone;
}

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

public void setNumber(Set&lt;Integer&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


&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;!-- Set --&gt;
&lt;bean id="CallingApp" class="com.codegeekslab.app.CallingApp"&gt;
&lt;property name="phone" ref="SmartPhone"&gt;&lt;/property&gt;
&lt;property name="number"&gt;
&lt;set&gt;
&lt;value&gt;12345&lt;/value&gt;
&lt;value&gt;67899&lt;/value&gt;
&lt;value&gt;12345&lt;/value&gt;
&lt;/set&gt;
&lt;/property&gt;
&lt;property name="app" value="whatsapp"&gt;&lt;/property&gt;
&lt;/bean&gt;
&lt;/beans&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 28, 2017 11:22:23 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
Mar 28, 2017 11:22:23 AM org.springframework.context.support.GenericXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericXmlApplicationContext@5e9f23b4: startup date [Tue Mar 28 11:22:23 IST 2017]; root of context hierarchy
calling 12345 via whatsapp
calling 67899 via whatsapp


As you can see from the output “calling via whatsapp number” is getting called only 2 times even though we are passing a Set of 3 numbers into the set method setNumber(Set<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. Set of Integer values using <property name=”number”><set> <value>12345</value><value>67899</value> <value>54321</value> </set></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>
<set>
<value>12345</value>
<value>67899</value>
<value>54321</value>
</set>
</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.Set;

import com.codegeekslab.device.Phone;

public class CallingApp {

private Phone phone;

private Set<Integer> number;

private String app;

public CallingApp(Phone phone, Set<Integer> number, String app) {
super();
this.phone = phone;
this.number = number;
this.app = app;
}

&nbsp;

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

}

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" />
<constructor-arg>
<set>
<value>12345</value>
<value>67899</value>
<value>12345</value>
</set>
</constructor-arg>
<constructor-arg value="whatsapp" />
</bean>

<!-- Set -->
<!-- <bean id="CallingApp" class="com.codegeekslab.app.CallingApp"> <property
name="phone" ref="SmartPhone"></property> <property name="number"> <set>
<value>12345</value> <value>67899</value> <value>12345</value> </set> </property>
<property name="app" value="whatsapp"></property> </bean> -->
</beans>

output


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

As like setter injection you can see from the output “calling via whatsapp number” is getting called only 2 times even though we are passing a Set of 3 numbers into the construtor public CallingApp(Phone phone, Set<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><set><value>12345</value><value>67899</value><value>54321</value></set></constructor-arg>.
  3. String Value using <constructor-arg value=”whatsapp”></constructor-arg>

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

Leave a Reply

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