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 object , In this example, we will pass a Set object 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.

Download this application -> Download

CellPhone.java


package com.codegeekslab.device;

import java.util.Set;

public interface CellPhone {

public void makeCall(Set<Integer> number, String app);

}

SmartPhone.java


package com.codegeekslab.type;

import java.util.Set;
import com.codegeekslab.device.CellPhone;

public class SmartPhone implements CellPhone {

public void makeCall(Set<Integer> 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.CellPhone;

public class BasicPhone implements CellPhone {

public void makeCall(Set<Integer> 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.CellPhone;

public class CallingApp {

private CellPhone phone;
private Set<Integer> number;
private String app;

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

public CellPhone getPhone() {
return phone;
}

public Set<Integer> getNumber() {
return number;
}

public void setNumber(Set<Integer> number) {
this.number = number;
}

public String getApp() {
return app;
}

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

public void dialNumber() {
phone.makeCall(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" />

<!-- setter injection of list -->
<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>

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();
}
}

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(class object,Set and String) 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.

Download this application -> Download

CallingApp.java


package com.codegeekslab.app;

import java.util.Set;
import com.codegeekslab.device.CellPhone;

public class CallingApp {

private CellPhone phone;
private Set<Integer> number;
private String app;
// for constructor injection
public CallingApp(CellPhone phone, Set<Integer> number, String app) {
super();
this.phone = phone;
this.number = number;
this.app = app;
}

public void dialNumber() {
phone.makeCall(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>

</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 constructor 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.