Constructor Dependency Injection with Collection of String example
In some scenarios, we may need to inject a Collection of String to an object rather than just one string value.
In such cases, we inject a Collection of string through a constructor.
Collection could be either List or Set but most of the requirements are best fit with List
Let us see how to inject a List of String through constructor.
Consider the example where one Person knows multiple languages
In this case, we are required to inject list of string to person object.
Let us see the same in spring through Constructor Dependency Injection
Create a class called Person
It contains 3 attributes id,name and languages where languages is a list of string
It also contains showPersonLanguages() method to display the list of languages 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<String> languages;
- public Person(int id, String name, List<String> languages) {
- super();
- this.id = id;
- this.name = name;
- this.languages = languages;
- }
- public void showPersonLanguages(){
- for (String language : languages) {
- System.out.println(language);
- }
- }
- }
package com.kb.di; import java.util.List; public class Person { private int id; private String name; private List<String> languages; public Person(int id, String name, List<String> languages) { super(); this.id = id; this.name = name; this.languages = languages; } public void showPersonLanguages(){ for (String language : languages) { System.out.println(language); } } }
Create a spring bean definition file
In this file, we used < constructor-arg > tag with to define the list of string.
< value > tag will take by default string values , for anything other than string , we need to specify the Type (we will see it in next topic)
- <?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">
- <constructor-arg value="1" type="int"></constructor-arg>
- <constructor-arg value="Raj"></constructor-arg>
- <constructor-arg>
- <list>
- <value>English</value>
- <value>Spanish</value>
- <value>German</value>
- <value>French</value>
- </list>
- </constructor-arg>
- </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"> <constructor-arg value="1" type="int"></constructor-arg> <constructor-arg value="Raj"></constructor-arg> <constructor-arg> <list> <value>English</value> <value>Spanish</value> <value>German</value> <value>French</value> </list> </constructor-arg> </bean> </beans>
If we need to inject Set rather than List, just change the above < list > tag to < set > tag.
And also make the languages as Set in Person class instead of List.
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 ConstructorDI class
This class loads the spring beans using application context and calls the person class’s method to display the languages.
- 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 ConstructorDI {
- public static void main(String[] args) {
- ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
- Person person = (Person)applicationContext.getBean("person");
- person. showPersonLanguages ();
- }
- }
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 ConstructorDI { public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml"); Person person = (Person)applicationContext.getBean("person"); person. showPersonLanguages (); } }