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

本文涉及的产品
注册配置 MSE Nacos/ZooKeeper,118元/月
应用实时监控服务-可观测链路OpenTelemetry版,每月50GB免费额度
任务调度 XXL-JOB 版免费试用,400 元额度,开发版规格
简介: 使用<property>标签的value属性配置原始数据类型和ref属性配置对象引用的方式来定义Bean配置文件。这两种情况都涉及将单一值传递给Bean

使用<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,关注后即可获取最新文章推送

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

相关文章
|
3月前
set集合
HashSet(无序,唯一): 基于 HashMap 实现的,底层采用 HashMap 来保存元素。 LinkedHashSet: LinkedHashSet 是 HashSet 的子类,并且其内部是通过 LinkedHashMap 来实现的。 TreeSet(有序,唯一): 红黑树(自平衡的排序二叉树)。
|
3月前
|
存储 Java 开发者
在 Java 中,如何遍历一个 Set 集合?
【10月更文挑战第30天】开发者可以根据具体的需求和代码风格选择合适的遍历方式。增强for循环简洁直观,适用于大多数简单的遍历场景;迭代器则更加灵活,可在遍历过程中进行更多复杂的操作;而Lambda表达式和`forEach`方法则提供了一种更简洁的函数式编程风格的遍历方式。
|
3月前
|
存储 Java
判断一个元素是否在 Java 中的 Set 集合中
【10月更文挑战第30天】使用`contains()`方法可以方便快捷地判断一个元素是否在Java中的`Set`集合中,但对于自定义对象,需要注意重写`equals()`方法以确保正确的判断结果,同时根据具体的性能需求选择合适的`Set`实现类。
|
3月前
|
Java 开发者
从 Java 中的 Set 集合中删除元素
【10月更文挑战第30天】
|
4月前
|
存储 NoSQL 关系型数据库
Redis 集合(Set)
10月更文挑战第17天
59 5
|
9月前
|
XML Java 数据格式
Spring5源码(6)-Spring注入集合属性
Spring5源码(6)-Spring注入集合属性
36 0
|
9月前
|
XML Java 数据格式
spring怎么去引用/注入集合/数组类型和 怎么通过 util 名称空间创建 list以及 怎么去通过级联属性赋值
spring怎么去引用/注入集合/数组类型和 怎么通过 util 名称空间创建 list以及 怎么去通过级联属性赋值
85 0
|
Java Spring
Spring-注入参数详解-[通过util命名空间简化集合类型的配置]
Spring-注入参数详解-[通过util命名空间简化集合类型的配置]
135 0
|
Java Spring 容器
Spring-注入参数详解-[集合类型属性]
Spring-注入参数详解-[集合类型属性]
90 0
|
存储 Java Spring
Spring框架中注入集合对象
你好看官,里面请!今天笔者讲的是在Spring框架中关于注入集合对象的用法(有示例!全网最详细!!) 不懂可以在评论区留言,我看到会及时回复。 注意:本文仅用与学习参考,不可用于商业用途。
816 3
Spring框架中注入集合对象