Spring Dependency Injection 简明教程

Spring DI - Collections Setter

您已经看到如何使用 Bean 配置文件中的 <property> 标记的 value 属性配置原始数据类型,以及使用 ref 属性配置对象引用。这两种情况都涉及将奇异值传递给 Bean。

You have seen how to configure primitive data type using value attribute and object references using ref attribute of the <property> tag in your Bean configuration file. Both the cases deal with passing singular value to a bean.

现在,如果您想传递 Java 集合类型之类的复数值,例如列表、集合、映射和属性,该怎么办。为了应对该情况,Spring 提供了以下类型的集合配置元素:

Now what if you want to pass plural values like Java Collection types such as List, Set, Map, and Properties. To handle the situation, Spring offers following types of collection configuration elements which are as follows −

Sr.No

Element & Description

1

<list> This helps in wiring ie injecting a list of values, allowing duplicates.

2

<set> This helps in wiring a set of values but without any duplicates.

3

<props> This can be used to inject a collection of name-value pairs where the name and value are both Strings.

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

You can use either <list> or <set> to wire any implementation of java.util.Collection or an array.

在该示例中,我们展示的是传递集合元素的直接值。

In this example, we’re showcasing passing direct values of the collection elements.

Example

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

The following example shows a class JavaCollection that is using collections as dependency injected using setters.

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

Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −

  1. JavaCollection.java − A class containing a collections as dependency.

  2. MainApp.java − Main application to run and test.

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

Here is the content of JavaCollection.java file −

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 文件的内容−

Following is the content of the MainApp.java file −

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 ,其中包含了所有类型的集合配置 −

Following is the configuration file applicationcontext.xml which has configuration for all the type of collections −

<?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>

Output

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

Once you are done creating the source and bean configuration files, let us run the application. If everything is fine with your application, it will print the following message −

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