灵活配置 Spring 集合:List、Set、Map、Properties 详解
使用
<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应用程序:
步骤 描述
- 创建一个名为SpringExample的项目,在创建的项目中的src文件夹下创建一个名为com.tutorialspoint的包。
- 使用"Add External JARs"选项添加所需的Spring库
- 在com.tutorialspoint包下创建Java类JavaCollection和MainApp。
- 在src文件夹下创建Beans配置文件Beans.xml。
- 最后一步是创建所有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,关注后即可获取最新文章推送
看完如果觉得有帮助,欢迎 点赞、收藏、关注
灵活配置 Spring 集合:List、Set、Map、Properties 详解的更多相关文章
- Spring集合 (List,Set,Map,Properties) 实例
下面例子向您展示Spring如何注入值到集合类型(List, Set, Map, and Properties). 支持4个主要的集合类型: List – <list/> Set – &l ...
- log4j.properties 详解与配置步骤(转)
找的文章,供参考使用 转自 log4j.properties 详解与配置步骤 一.log4j.properties 的使用详解 1.输出级别的种类 ERROR.WARN.INFO.DEBUGERROR ...
- C++中的STL中map用法详解(转)
原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解 Map是STL的一个关联容器,它提供 ...
- SpringBoot配置文件 application.properties详解
SpringBoot配置文件 application.properties详解 本文转载:https://www.cnblogs.com/louby/p/8565027.html 阅读过程中若发现 ...
- 2017.2.13 开涛shiro教程-第十二章-与Spring集成(一)配置文件详解
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(一)配置文件详解 1.pom.xml ...
- hibernate集合映射inverse和cascade详解
hibernate集合映射inverse和cascade详解 1.到底在哪用cascade="..."? cascade属性并不是多对多关系一定要用的,有了它只是让我们在插入或 ...
- GoLang基础数据类型--->字典(map)详解
GoLang基础数据类型--->字典(map)详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 可能大家刚刚接触Golang的小伙伴都会跟我一样,这个map是干嘛的,是 ...
- Jquery遍历筛选数组的几种方法和遍历解析json对象|Map()方法详解
Jquery遍历筛选数组的几种方法和遍历解析json对象|Map()方法详解 一.Jquery遍历筛选数组 1.jquery grep()筛选遍历数组 $().ready( function(){ v ...
- Spring Boot的每个模块包详解
Spring Boot的每个模块包详解,具体如下: 1.spring-boot-starter 这是Spring Boot的核心启动器,包含了自动配置.日志和YAML. 2.spring-boot-s ...
- Spring Boot源码中模块详解
Spring Boot源码中模块详解 一.源码 spring boot2.1版本源码地址:https://github.com/spring-projects/spring-boot/tree/2.1 ...
随机推荐
- ZYNQ核心板及其底板开源啦!
Hello-FPGA ZYNQ 设计开源啦! 开源ZYNQ核心板 + 底板 硬件设计.软件设计,软件设计使用裸机演示,演示了如何使用AXI DMA等关键dma 模块 欢迎加QQ 讨论 94755958 ...
- CXP协议的传输层介绍 8b/10b编码
8b/10b编码与K码 upconnection 和downconnection均使用8b/10b编码,因此我们先简单回顾一下8b/10b吧 8B/10B编码被广泛应用到高速串行总线,如IEEE139 ...
- 监控系统open-falcon安装部署
目录 官方文档 安装包下载地址 环境准备 安装redis 安装mysql 安装git 安装GO 安装后台 部署前端: 邮件报警 修改ALARM配置 修改报警接口 报警测试邮件展示 Q&A 官方 ...
- Avalonia 跨平台框架中如何以正确的姿势使用 OpenGL ES 渲染。
前言 这是一篇很水的文章,没有任何技术含量,在 Github 已经有很多人对 Avalonia 的 OpenGL ES 支持进行了讨论,但是我还是想写一篇文章来记录一下我是如何在 Avalonia 中 ...
- 【Azure 服务总线】Azure.Messaging.ServiceBus 多次发送消息报超时错误,是否可以配置重新发送?是否有内置重试机制?
问题描述 使用 Azure Service Bus,提供应用程序之间松耦合的消息交换,但是有时候发送消息多次出现超时错误. A connection attempt failed because th ...
- 【Azure 媒体服务】记录使用Java调用Media Service API时候遇见的一些问题
问题一:java.lang.IllegalArgumentException: Parameter this.client.subscriptionId() is required and canno ...
- Java 在三个数字中找出最大值
1 int aa1 = 11000000; 2 int aa2 = 20000; 3 int aa3 = 6000; 4 5 //第一种 6 int max = (aa1 > aa2)? aa1 ...
- Cordova下载文件,监听进度,退出疯狂报错
如题. 报错如下: W/cr_AwContents: Application attempted to call on a destroyed WebView java.lang.Throwable ...
- 排查 dotNET Core 程序内存暴涨的问题
0. 问题 新版本上线之后,发现内存猛涨,入站流量猛增,不清楚具体原因,部分接口提示 OOM 异常,随后 Pod 直接崩溃无限重启. 1. 准备 Pod 已经接入了 NewRelic 和 Graylo ...
- SSH原理与实践(三)安装和使用
主页 个人微信公众号:密码应用技术实战 个人博客园首页:https://www.cnblogs.com/informatics/ 引言 在之前SSH原理与实践系列文章中,我们主要讲解了SSH协议的原理 ...