灵活配置 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 ...
随机推荐
- 狂神说Git学习笔记整理
Git 版本控制 在开发过程中,项目会进行版本迭代,新版本会取代旧版本,但是我们不希望直接删除旧版本,所以就需要一个版本管理器来管理新旧版本,不然就是手动控制... 多人开发必须使用版本控制!!! ...
- 制作docker方式执行Gitlab Runner所需要的镜像
背景知识 启动Gitlab Runner时,使用Gitlab提供的官方镜像gitlab/gitlab-runner:latest即可. Runner以容器的方式启动以后,根据前文我们注册到Gitlab ...
- 【Azure 应用服务】更便捷的方式抓取Azure App Service for Windows的网络包
问题描述 在之前的一篇博文中,介绍了在App Service中抓取网络日志: 抓取Windows的网络包:[应用服务 App Service]App Service中抓取网络日志 抓取Linux的网络 ...
- 浅入Kubernetes(13):dashboard、api、访问配置
目录 Kubectl 命令大全 安装 Kubernetes-Dashboard RESTful API 鉴权 注解 Pod YAML 结构 配置 Kubectl 命令大全 在前面,我们学习到了一些 K ...
- 可视化探索开源项目的 contributor 关系
引语:作为国内外最大的代码托管平台,根据最新的 GitHub 数据,它拥有超 372,000,000 个仓库,其中有 28,000,000 是公开仓.分布式图数据库 NebulaGraph 便是其中之 ...
- [Python] 子线程退出孙线程不退出
遇到了一个大坑! 如图,在子线程ThreadFunc退出之后,ThreadFunc2依旧在运行... 根本不会结束 但是官方文档中说明了,只要设置了daemon不为None 就能设置子线程是守护线程, ...
- [C++] 进程注入dll版
目录 前言 前提 原理 代码 一些问题 前言 这几天在弄进程注入的事情,一直做不出来直接的进程注入,也就是不要dll的注入.因为dll注入据说容易触发杀软,但是弄了两天没弄出来.代码明明不报错,目标进 ...
- FreeRTOS教程1 基础知识
1.准备材料 正点原子stm32f407探索者开发板V2.4 STM32CubeMX软件(Version 6.10.0) Keil µVision5 IDE(MDK-Arm) 野火DAP仿真器 2.学 ...
- IDEA使用与多线程
IDEA缩写和快捷键 psvm全称public static void main sout 全称public static void main alt+enter 处理异常 s.out 自动打印s c ...
- vscode 快速切换窗口 快捷键 设置成 Alt + Q 了
vscode 切换窗口 快捷键 设置成 Alt + Q 了 又换了 换成 快速切换窗口了 quickSwitchWindow 这样方便了 我再感受下一