spring——Spring 注入集合(转载)
我们还可以在 Bean 标签下的 <property> 元素中,使用以下元素配置 Java 集合类型的属性和参数,例如 List、Set、Map 以及 Properties 等。
标签 | 说明 |
---|---|
<list> | 用于注入 list 类型的值,允许重复 |
<set> | 用于注入 set 类型的值,不允许重复 |
<map> | 用于注入 key-value 的集合,其中 key 和 value 都可以是任意类型 |
<props> | 用于注入 key-value 的集合,其中 key 和 value 都是字符串类型 |
示例 1:在集合中设置普通类型的值
下面我们通过一个实例,演示下如何注入集合类型的属性和参数。
1. 参考《第一个 Spring 程序》,新建一个名为 my-spring-demo4 的 Java 项目。
2. 在 net.biancheng.c 包下,创建一个名为 JavaCollection 的类,代码如下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package net.biancheng.c; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; public class JavaCollection { //1 数组类型属性 private String[] courses; //2 list 集合类型属性 private List<String> list; //3 map 集合类型属性 private Map<String, String> maps; //4 set 集合类型属性 private Set<String> sets; public void setCourses(String[] courses) { this .courses = courses; } public void setList(List<String> list) { this .list = list; } public void setMaps(Map<String, String> maps) { this .maps = maps; } public void setSets(Set<String> sets) { this .sets = sets; } @Override public String toString() { return "JavaCollection{" + "courses=" + Arrays.toString(courses) + ", list=" + list + ", maps=" + maps + ", sets=" + sets + '}' ; } } |
2. 在 src 目录下创建 Spring 配置文件 Beans.xml,配置如下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <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 id= "javaCollection" class = "net.biancheng.c.JavaCollection" > <!--数组类型--> <property name= "courses" > <array> <value>Java</value> <value>PHP</value> <value>C 语言</value> </array> </property> <!--List 类型--> <property name= "list" > <list> <value>张三</value> <value>李四</value> <value>王五</value> <value>赵六</value> </list> </property> <!--Map 类型--> <property name= "maps" > <map> <entry key= "JAVA" value= "java" ></entry> <entry key= "PHP" value= "php" ></entry> </map> </property> <!--Set 类型--> <property name= "sets" > <set> <value>MySQL</value> <value>Redis</value> </set> </property> </bean> </beans> |
============================================================
示例 2:在集合中设置对象类型的值
在上面的示例中,都是在集合中通过 value 属性设置的普通类型的值,我们还可以通过 ref 属性在注入到 Bean 的集合中设置对象类型的值。
1. 在 my-spring-demo4 项目的 net.biancheng.c 包中,创建一个名为 Course 的类,代码如下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package net.biancheng.c; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class Course { private static final Log LOGGER = LogFactory.getLog(Course. class ); //课程编号 private Integer courseId; //课程名称 private String courseName; public void setCourseId(Integer courseId) { this .courseId = courseId; } public void setCourseName(String courseName) { this .courseName = courseName; } @Override public String toString() { return "Course{" + "courseId=" + courseId + ", courseName='" + courseName + '\ '' + '}' ; } } |
2. 将 JavaCollection 中的代码修改成以下形式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package net.biancheng.c; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; public class JavaCollection { //1 数组类型属性 private Course[] courses; //2 list 集合类型属性 private List<String> list; //3 map 集合类型属性 private Map<String, String> maps; //4 set 集合类型属性 private Set<String> sets; public void setCourses(Course[] courses) { this .courses = courses; } public void setList(List<String> list) { this .list = list; } public void setMaps(Map<String, String> maps) { this .maps = maps; } public void setSets(Set<String> sets) { this .sets = sets; } @Override public String toString() { return "JavaCollection{" + "courses=" + Arrays.toString(courses) + ", list=" + list + ", maps=" + maps + ", sets=" + sets + '}' ; } } |
3. 将 Beans.xml 中配置修改成以下内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <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 id= "course" class = "net.biancheng.c.Course" > <property name= "courseId" value= "1" ></property> <property name= "courseName" value= "Java课程" ></property> </bean> <bean id= "course2" class = "net.biancheng.c.Course" > <property name= "courseId" value= "2" ></property> <property name= "courseName" value= "PHP课程" ></property> </bean> <bean id= "course3" class = "net.biancheng.c.Course" > <property name= "courseId" value= "3" ></property> <property name= "courseName" value= "C语言课程" ></property> </bean> <bean id= "javaCollection" class = "net.biancheng.c.JavaCollection" > <!--数组类型--> <property name= "courses" > <array> <ref bean= "course" ></ref> <ref bean= "course2" ></ref> <ref bean= "course3" ></ref> </array> </property> <!--List 类型--> <property name= "list" > <list> <value>张三</value> <value>李四</value> <value>王五</value> <value>赵六</value> </list> </property> <!--Map 类型--> <property name= "maps" > <map> <entry key= "JAVA" value= "java" ></entry> <entry key= "PHP" value= "php" ></entry> </map> </property> <!--Set 类型--> <property name= "sets" > <set> <value>MySQL</value> <value>Redis</value> </set> </property> </bean> </beans> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2021-04-17 自动化——测试开发进阶——flask——flask-request对象
2021-04-17 自动化——测试开发进阶——flask——flask为路由指定HTTP方法
2021-04-17 自动化——测试开发进阶——flask——flask变量规则与url构建