Spring DI - 集合 Setter

您已经了解了如何使用 Bean 配置文件中的 <property> 标签的 value 属性和对象引用来配置原始数据类型和对象引用。 这两种情况都处理将奇异值传递给 bean。

现在,如果您想传递多个值,例如 Java 集合类型,如 List、Set、Map 和 Properties,该怎么办。 为了处理这种情况,Spring 提供了以下类型的集合配置元素,如下所示 −

序号 元素 & 描述
1

<list>

这有助于连接,即注入一个值列表,允许重复。

2

<set>

这有助于连接一组值,但没有任何重复。

3

<props>

这可用于注入名称和值都是字符串的 name-value 对集合。

您可以使用 <list> 或 <set> 来连接 java.util.Collection 或 array 的任何实现。

在这个例子中,我们展示了传递集合元素的直接值。


示例

以下示例显示了一个类 JavaCollection,它使用集合作为使用 setter 注入的依赖项。

让我们更新Spring DI - 创建项目章节中创建的项目。 我们正在添加以下文件 −

  • JavaCollection.java − 包含作为依赖项的集合的类。

  • MainApp.java − 要运行和测试的主应用程序。

这是 JavaCollection.java 文件的内容 −


package com.tutorialspoint;
import java.util.*;

public class JavaCollection {
   List<String> addressList;
   Set<String>  addressSet;
   Properties addressProp;

   // a setter method to set List
   public void setAddressList(List<String> addressList) {
      this.addressList = addressList;
   }
   
   // prints and returns all the elements of the list.
   public List<String> getAddressList() {
      System.out.println("List Elements :"  + addressList);
      return addressList;
   }
   
   // a setter method to set Set
   public void setAddressSet(Set<String> addressSet) {
      this.addressSet = addressSet;
   }
   
   // prints and returns all the elements of the Set.
   public Set<String> getAddressSet() {
      System.out.println("Set Elements :"  + addressSet);
      return addressSet;
   }
   
   // a setter method to set Property
   public void setAddressProp(Properties addressProp) {
      this.addressProp = addressProp;
   }
   
   // prints and returns all the elements of the Property.
   public Properties getAddressProp() {
      System.out.println("Property Elements :"  + addressProp);
      return addressProp;
   }
}

以下是 MainApp.java 文件的内容 −


package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
      JavaCollection jc=(JavaCollection)context.getBean("javaCollection");

      jc.getAddressList();
      jc.getAddressSet();
      jc.getAddressProp();
   }
}

以下是配置文件 applicationcontext.xml,其中包含所有集合类型的配置 −


<?xml version = "1.0" encoding = "UTF-8"?>

<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-3.0.xsd">

   <!-- Definition for javaCollection -->
   <bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection">
      <!-- results in a setAddressList(java.util.List) call -->
      <property name = "addressList">
         <list>
            <value>INDIA</value>
            <value>JAPAN</value>
            <value>USA</value>
            <value>UK</value>
         </list>
      </property>

      <!-- results in a setAddressSet(java.util.Set) call -->
      <property name = "addressSet">
         <set>
            <value>INDIA</value>
            <value>JAPAN</value>
            <value>USA</value>
            <value>UK</value>
         </set>
      </property>
      
      <!-- results in a setAddressProp(java.util.Properties) call -->
      <property name = "addressProp">
         <props>
            <prop key = "one">INDIA</prop>
            <prop key = "two">JAPAN</prop>
            <prop key = "three">USA</prop>
            <prop key = "four">UK</prop>
         </props>
      </property>
   </bean>
</beans>

输出

创建完源和 bean 配置文件后,让我们运行应用程序。 如果您的应用程序一切正常,它将打印以下消息 −

List Elements :[INDIA, JAPAN, USA, UK]
Set Elements :[INDIA, JAPAN, USA, UK]
Property Elements :{four=UK, one=INDIA, two=JAPAN, three=USA}