Spring DI - 注入集合类型构造函数



您已经了解了如何在 Bean 配置文件中使用value属性配置基本数据类型,以及使用<property>标签的ref属性配置对象引用。这两种情况都涉及将单个值传递给 Bean。

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

序号 元素及描述
1

<list>

这有助于连接(即注入)一系列值,允许重复。

2

<set>

这有助于连接一系列值,但不能重复。

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

在本例中,我们将展示如何使用 ref 传递集合元素。

示例

以下示例显示了一个名为JavaCollection的类,它使用通过 Setter 方法注入的依赖项集合。

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

  • Address.java - 用作依赖项的类。

  • JavaCollection.java - 包含依赖项集合的类。

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

以下是Address.java文件的内容:

package com.tutorialspoint;

public class Address {
   private String name;

   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }	
   @Override
   public String toString() {
      return name;
   }
}

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

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

public class JavaCollection {
   List<Address> addressList;
   Set<Address>  addressSet;

   public JavaCollection(List<Address> addressList, Set<Address> addressSet) {
      this.addressList = addressList;
      this.addressSet = addressSet;
   }

   // a setter method to set List
   public void setAddressList(List<Address> addressList) {
      this.addressList = addressList;
   }

   // prints and returns all the elements of the list.
   public List<Address> getAddressList() {
      System.out.println("List Elements :"  + addressList);
      return addressList;
   }

   // a setter method to set Set
   public void setAddressSet(Set<Address> addressSet) {
      this.addressSet = addressSet;
   }
   
   // prints and returns all the elements of the Set.
   public Set<Address> getAddressSet() {
      System.out.println("Set Elements :"  + addressSet);
      return addressSet;
   }
}

以下是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();
   }
}

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

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

   <bean id = "address1" class = "com.tutorialspoint.Address">
      <property name="name" value="INDIA"></property>
   </bean>
   <bean id = "address2" class = "com.tutorialspoint.Address">
      <property name="name" value="JAPAN"></property>
   </bean>
   <bean id = "address3" class = "com.tutorialspoint.Address">
      <property name="name" value="USA"></property>
   </bean>
   <bean id = "address4" class = "com.tutorialspoint.Address">
      <property name="name" value="UK"></property>
   </bean>
   <!-- Definition for javaCollection -->
   <bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection">
      <constructor-arg name = "addressList">
         <list>
            <ref bean="address1" />
            <ref bean="address2" />
            <ref bean="address3" />
            <ref bean="address4" />
         </list>
      </constructor-arg>
      <constructor-arg name = "addressSet">
         <set>
            <ref bean="address1" />
            <ref bean="address2" />
            <ref bean="address3" />
            <ref bean="address4" />
         </set>
      </constructor-arg>
   </bean>
</beans>

输出

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

List Elements :[INDIA, JAPAN, USA, UK]
Set Elements :[INDIA, JAPAN, USA, UK]
广告