灵活配置 Spring 集合:List、Set、Map、Properties 详解

小万哥
• 阅读 1216

使用<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库
  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)

最后

为了方便其他设备和平台的小伙伴观看往期文章:

微信公众号搜索:Let us Coding,关注后即可获取最新文章推送

看完如果觉得有帮助,欢迎 点赞、收藏、关注

点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
java 11 不可修改集合API
不可修改集合API自Java9开始,Jdk里面为集合(List/Set/Map)都添加了of和copyOf方法,它们两个都用来创建不可变的集合,来看下它们的使用和区别。示例1:varlistList.of("Java","Python","C");varcopyList.copy
Wesley13 Wesley13
2年前
java8新特性
Stream将List转换为Map,使用Collectors.toMap方法进行转换背景:User类,类中分别有id,name,age三个属性。List集合,userList,存储User对象1、指定keyvalue,value是对象中的某个属性值。 Map<Integer,StringuserMap1userList.str
zdd小小菜鸟 zdd小小菜鸟
1年前
Java基础面试
Java基础面试1.Java集合类的总结tex1、Iterator:Collection(值)、Map(键值对);2、Collection:Set(无序不重复)、List(有序可重复)、Queue;3、Set:H
Easter79 Easter79
2年前
Spring引用properties文件
  Srping提供了PropertyPlaceholderConfigurer,它是一个容器后处理器,负责读取properties属性文件里的属性值,并将这些属性值设置成Spring配置文件的元数据。如定义Properties类:packagecom.custle.spring;/Created
Stella981 Stella981
2年前
Spring 学习笔记(三):Spring Bean
1Bean配置Spring可以看做是一个管理Bean的工厂,开发者需要将Bean配置在XML或者Properties配置文件中。实际开发中常使用XML的格式,其中<bean中的属性或子元素如下:id:Bean在BeanFactory中的唯一标识,在代码中通过BeanFac
Wesley13 Wesley13
2年前
System、Runtime、Date、Calendar、Math
System类中的方法和属性都是静态的。out:标准输出,默认是控制台in:标准输入,默认是键盘获取系统属性信息:PropertiesgetProperties();因为Properties是Hashtable的,也就是Map集合的子类对象,那么可以通过map的方法取出集合中的元素,该集合中存储都是字符串,没有泛型定义。setPr
Stella981 Stella981
2年前
Redis的安装配置和使用
  现在我来讲解一下Redis的安装和配置,那么什么是Redis呢?他的作用是什么呢?  redis是一个keyvalue存储系统,和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sortedset有序集合)和hash(哈希类型)。这些数据类型都支持push/
Easter79 Easter79
2年前
Spring高级应用之注入嵌套Bean
在Spring中,如果某个Bean所依赖的Bean不想被Spring容器直接访问,可以使用嵌套Bean。和普通的Bean一样,使用<bean元素来定义嵌套的Bean,嵌套Bean只对它的外部的Bean有效,Spring容器无法直接访问嵌套的Bean,因此定义嵌套Bean也无需指定id属性。如下配置片段是一个嵌套Bean的示例:<bean id
Wesley13 Wesley13
2年前
Java核心(四)你不知道的数据集合
!数据容器关系图(http://icdn.apigo.cn/blog/collection002.png)导读:Map竟然不属于Java集合框架的子集?队列也和List一样属于集合的三大子集之一?更有队列的正确使用姿势,一起来看吧!Java中的集合通常指的是Collection下的三个集合框架List、Set、Queue和Map集合,Map并不
Wesley13 Wesley13
2年前
Java方面技术点小整理
Java中的集合吗?java中的集合分为value、keyvalueg两种存储值有分为list和setList有序的,可以重复Set是序的,不可以重复的根据equals和hashCode判断如果一个对象要存储在set中,必须重写equals和hashCode的方法;存储keyvalue的为map