Setter Dependency Injection with List of Integer/Long example
In some scenarios, we may need to inject a list of Integers/Long to an object rather than just one Integer/Long value.
Consider the example where one Person has multiple phone numbers
In this case, we are required to inject list of Long object to person object.
Let us see the same in spring through Setter Dependency Injection
Create a class called Person
It contains 3 attributes id,name and phoneNumbers where phoneNumbers is a list of Long
It contains 3 setter methods one for each attribute.
It also contains showPersonPhoneNumbers() method to display the list of phoneNumbers that we have injected to the person object.
- package com.kb.di;
- import java.util.List;
- public class Person {
- private int id;
- private String name;
- private List<Long> phoneNumbers;
- public void showPersonPhoneNumbers() {
- for (Long phoneNumber : phoneNumbers) {
- System.out.println(phoneNumber);
- }
- }
- public void setId(int id) {
- this.id = id;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setPhoneNumbers(List<Long> phoneNumbers) {
- this.phoneNumbers = phoneNumbers;
- }
- }
package com.kb.di; import java.util.List; public class Person { private int id; private String name; private List<Long> phoneNumbers; public void showPersonPhoneNumbers() { for (Long phoneNumber : phoneNumbers) { System.out.println(phoneNumber); } } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setPhoneNumbers(List<Long> phoneNumbers) { this.phoneNumbers = phoneNumbers; } }
Create a spring bean definition file
In this file, we used < property > tag with < list > to define the list of Long.
< value > tag is used to inject the values
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
- <bean id="person" class="com.kb.di.Person">
- <property name="id" value="1"></property>
- <property name="name" value="Raj"></property>
- <property name="phoneNumbers">
- <list>
- <value>9999912345</value>
- <value>9999956789</value>
- </list>
- </property>
- </bean>
- </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <bean id="person" class="com.kb.di.Person"> <property name="id" value="1"></property> <property name="name" value="Raj"></property> <property name="phoneNumbers"> <list> <value>9999912345</value> <value>9999956789</value> </list> </property> </bean> </beans>
Create a pom.xml file
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>Spring</groupId>
- <artifactId>SpringCore</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>SpringCore</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>4.2.4.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>4.2.4.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Spring</groupId> <artifactId>SpringCore</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>SpringCore</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>
Create a SetterDI class
This class loads the spring beans using application context and calls the person class method to display the list of phone numbers.
- package com.kb.di;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.beans.factory.xml.XmlBeanFactory;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.core.io.Resource;
- public class SetterDI {
- public static void main(String[] args) {
- ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
- Person person = (Person)applicationContext.getBean("person");
- person.showPersonPhoneNumbers();
- }
- }
package com.kb.di; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class SetterDI { public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml"); Person person = (Person)applicationContext.getBean("person"); person.showPersonPhoneNumbers(); } }
Run the SetterDI class and see the output as below
same can be achieved for Integers just by changing Long to Integer in Person class.