Enjoying This Tutorial?
Subscribe to Ram N Java for high-quality, practical Java and Spring tutorials!
SUBSCRIBE NOWIntroduction
In Spring Framework, injecting collections is a common design pattern for managing lists, sets, and maps without writing boilerplate code. When your application requires a collection of distinct literal values (such as strings, integers, or codes) where duplicates should be disregarded, Spring provides the dedicated <set> element to inject a java.util.Set via setter-based dependency injection.
Why Inject a Set Collection in Spring?
- Ensures Data Uniqueness: A
Setautomatically suppresses duplicate entries, ensuring your bean works strictly with unique elements. - Declarative XML Configuration: The
<set>and<value>tags make it straightforward to configure collection values in your XML configuration file. - Loosely Coupled Design: Changing or extending the configured values requires only editing your Spring XML without modifying the underlying Java classes.
Step-by-Step Code Example
Here is an example showing how to inject a Set<String> of phone numbers into a Customer bean using setter injection:
1. The Java Model Class
package com.ram.model;
import java.util.Set;
public class Customer {
private String name;
private Set<String> phoneNumbers;
public void setName(String name) {
this.name = name;
}
// Setter method for Set injection
public void setPhoneNumbers(Set<String> phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
public void display() {
System.out.println("Customer Name: " + name);
System.out.println("Phone Numbers: " + phoneNumbers);
}
}
2. Configuring applicationContext.xml
Inside the <property> element, define the values using the <set> and <value> tags:
<bean id="customer" class="com.ram.model.Customer">
<property name="name" value="Ram" />
<property name="phoneNumbers">
<set>
<value>9876543210</value>
<value>9123456780</value>
<value>9876543210</value> <!-- Duplicate values are eliminated -->
</set>
</property>
</bean>
3. Loading and Executing with ApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Customer customer = (Customer) context.getBean("customer");
customer.display();
Key Takeaway
Spring handles the instantiation and population of java.util.Set collections seamlessly using the <set> element, allowing you to pass unique configuration values cleanly via setter dependency injection.
Recommended Collection & Dependency Injection Tutorials from Ram N Java
Click here to watch on Youtube:
https://www.youtube.com/watch?v=bRtnP9hl9bc&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge:
| How to Inject Set Collection using Spring dependency injection by Setter method? | Spring Tutorial |
| How to Inject Set Collection using Spring dependency injection by Setter method? | Spring Tutorial |
| How to Inject Set Collection using Spring dependency injection by Setter method? | Spring Tutorial |
| How to Inject Set Collection using Spring dependency injection by Setter method? | Spring Tutorial |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0%20http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ram.core</groupId>
<artifactId>SpringDemo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringDemo</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>5.0.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring 5 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
FruitShop.java
import java.util.Set;
public class FruitShop
{
private String shopName;
private Set<String> availableFruitsSet;
public String getShopName()
{
return shopName;
}
public void setShopName(String shopName)
{
System.out.println("setShopName(String shopName) is "
+ "called by the Spring Container");
this.shopName = shopName;
}
public Set<String> getAvailableFruitsSet()
{
return availableFruitsSet;
}
public void setAvailableFruitsSet(Set<String> availableFruitsSet)
{
System.out.println(
"setAvailableFruitsSet(Set<String> availableFruitsSet) is "
+ "called by the Spring Container");
this.availableFruitsSet = availableFruitsSet;
}
public void displayFruitShopInfo()
{
System.out.println("FruitShop [shopName=" + shopName
+ ", availableFruitsSet=" + availableFruitsSet + "]");
}
}
applicationContext.xml
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-4.3.xsd">
<bean id="fruitShop" class="com.ram.core.FruitShop">
<property name="shopName" value="Fruit Mart"></property>
<property name="availableFruitsSet">
<set>
<value>apple</value>
<value>pineapple</value>
<value>orange</value>
</set>
</property>
</bean>
</beans>
App.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App
{
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
System.out.println("---------------------------------------");
FruitShop fruitShop = (FruitShop) context.getBean("fruitShop");
System.out.println(
"Got fruitShop object from the ApplicationContext(SpringContainer)");
fruitShop.displayFruitShopInfo();
}
}
Output
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5b37e0d2: startup date [Sat May 12 08:55:28 IST 2018]; root of context hierarchy
May 12, 2018 8:55:28 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
setShopName(String shopName) is called by the Spring Container
setAvailableFruitsSet(Set<String> availableFruitsSet) is called by the Spring Container
---------------------------------------
Got fruitShop object from the ApplicationContext(SpringContainer)
FruitShop [shopName=Fruit Mart, availableFruitsSet=[apple, pineapple, orange]]
No comments:
Post a Comment