为了账号安全,请及时绑定邮箱和手机立即绑定

Spring 注入集合

使用<property>标签的value属性配置原始数据类型和ref属性配置对象引用的方式来定义Bean配置文件。这两种情况都涉及将单一值传递给Bean。那么如果您想传递多个值,例如Java集合类型,如List、Set、Map和Properties怎么办?为了处理这种情况,Spring提供了四种类型的集合配置元素,如下所示:

序号 元素 & 描述
1 <list>
用于注入一组值,允许重复。
2 <set>
用于注入一组值,但不允许重复。
3 <map>
可用于注入一组名称-值对,其中名称和值可以是任何类型。
4 <props>
可用于注入一组名称-值对,其中名称和值都是字符串。

您可以使用<list><set>来注入java.util.Collection的任何实现或数组。

在处理集合时,通常会遇到两种情况:(a)传递集合的直接值和(b)将Bean的引用作为集合元素之一传递。

示例

假设您已经准备好Eclipse IDE,并采取以下步骤创建Spring应用程序:

步骤 描述

1 创建一个名为SpringExample的项目,在创建的项目中的src文件夹下创建一个名为com.tutorialspoint的包。

2 使用"Add External JARs"选项添加所需的Spring库,如Spring Hello World示例章节中所述。

3 在com.tutorialspoint包下创建Java类JavaCollection和MainApp。

4 在src文件夹下创建Beans配置文件Beans.xml。

5 最后一步是创建所有Java文件和Bean配置文件的内容,并按以下说明运行应用程序。

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

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

public class JavaCollection {
   List addressList;
   Set  addressSet;
   Map  addressMap;
   Properties addressProp;

   // 用于设置List的setter方法
   public void setAddressList(List addressList) {
      this.addressList = addressList;
   }
   
   // 打印并返回列表的所有元素。
   public List getAddressList() {
      System.out.println("List Elements :"  + addressList);
      return addressList;
   }
   
   // 用于设置Set的setter方法
   public void setAddressSet(Set addressSet) {
      this.addressSet = addressSet;
   }
   
   // 打印并返回Set的所有元素。
   public Set getAddressSet() {
      System.out.println("Set Elements :"  + addressSet);
      return addressSet;
   }
   
   // 用于设置Map的setter方法
   public void setAddressMap(Map addressMap) {
      this.addressMap = addressMap;
   }
   
   // 打印并返回Map的所有元素。
   public Map getAddressMap() {
      System.out.println("Map Elements :"  + addressMap);
      return addressMap;
   }
   
   // 用于设置Property的setter方法
   public void setAddressProp(Properties addressProp) {
      this.addressProp = addressProp;
   }
   
   // 打印并返回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("Beans.xml");
      JavaCollection jc=(JavaCollection)context.getBean("javaCollection");

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

以下是包含所有集合类型配置的Beans.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">
      
      <!-- 产生 setAddressList(java.util.List) 调用 -->
      <property name = "addressList">
         <list>
            <value>INDIA</value>
            <value>Pakistan</value>
            <value>USA</value>
            <value>USA</value>
         </list>
      </property>

      <!-- 产生 setAddressSet(java.util.Set) 调用 -->
      <property name = "addressSet">
         <set>
            <value>INDIA</value>
            <value>Pakistan</value>
            <value>USA</value>
            <value>USA</value>
         </set>
      </property>

      <!-- 产生 setAddressMap(java.util.Map) 调用 -->
      <property name = "addressMap">
         <map>
            <entry key = "1" value = "INDIA"/>
            <entry key = "2" value = "Pakistan"/>
            <entry key = "3" value = "USA"/>
            <entry key = "4" value = "USA"/>
         </map>
      </property>
      
      <!-- 产生 setAddressProp(java.util.Properties) 调用 -->
      <property name = "addressProp">
         <props>
            <prop key = "one">INDIA</prop>
            <prop key = "one">INDIA</prop>
            <prop key = "two">Pakistan</prop>
            <prop key = "three">USA</prop>
            <prop key = "four">USA</prop>
         </props>
      </property>
   </bean>

</beans>

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

List Elements :[INDIA, Pakistan, USA, USA]
Set Elements :[INDIA, Pakistan, USA]
Map Elements :{1=INDIA, 2=Pakistan, 3=USA, 4=USA}
Property Elements :{two=Pakistan, one=INDIA, three=USA, four=USA}

注入Bean引用

以下Bean定义将帮助您了解如何将Bean引用注入为集合的元素之一。您甚至可以将引用和值混合在一起,如下面的代码片段所示:

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

   <!-- Bean Definition to handle references and values -->
   <bean id = "..." class = "...">

      <!-- Passing bean reference  for java.util.List -->
      <property name = "addressList">
         <list>
            <ref bean = "address1"/>
            <ref bean = "address2"/>
            <value>Pakistan</value>
         </list>
      </property>
      
      <!-- Passing bean reference  for java.util.Set -->
      <property name = "addressSet">
         <set>
            <ref bean = "address1"/>
            <ref bean = "address2"/>
            <value>Pakistan</value>
         </set>
      </property>
      
      <!-- Passing bean reference  for java.util.Map -->
      <property name = "addressMap">
         <map>
            <entry key = "one" value = "INDIA"/>
            <entry key = "two" value-ref = "address1"/>
            <entry key = "three" value-ref = "address2"/>
         </map>
      </property>
   </bean>

</beans>

要使用上述Bean定义,您需要以使它们能够处理引用的方式定义setter方法。

注入null和空字符串值

如果需要传递空字符串作为值,可以使用以下方式传递:

<bean id = "..." class = "exampleBean">
   <property name = "email" value = ""/>
</bean>

上述示例等效于Java代码:exampleBean.setEmail("")

如果需要传递NULL值,可以使用以下方式传递:

<bean id = "..." class = "exampleBean">
   <property name = "email"><null/></property>
</bean>

上述示例等效于Java代码:exampleBean.setEmail(null)

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消