【spring框架】集合属性注入方法
1 前言
本博客主要介绍spring框架中,注入集合属性的方法,主要包含 List 属性注入方法、 Set 属性注入方法、 Map 属性注入方法。在测试时,统一使用如下代码:
Test.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Student s=ac.getBean("student",Student.class);
System.out.println(s);
}
}
2 List 属性注入方法
本节主要介绍 List 类型属性注入,对于Array、Set 等类型属性注入方法,和 List 类型属性注入方法类似,只需要把applicationContext.xml 文件中的 <list> 标签改为 <array>、<set> 等。
需要注入属性的对象为 Student 类,如下:
Student.java
import java.util.List;
public class Student {
private String sname;
private List teachers;
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public List getTeachers() {
return teachers;
}
public void setTeachers(List teachers) {
this.teachers = teachers;
}
@Override
public String toString() {
return "Student [sname=" + sname + ", teachers=" + teachers + "]";
}
}
2.1 List 元素为字面量
若 Student 类中 teachers 属性为字面量(基本数据类型及其封装类),beans 配置文件如下:
applicationContext.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.xsd">
<bean id="student" class="com.test.Student">
<property name="sname" value="小王"></property>
<property name="teachers">
<list>
<value>A</value>
<value>B</value>
</list>
</property>
</bean>
</beans>
运行结果:
Student [sname=小王, teachers=[A, B]]
使用 util 命名空
使用 util 命名空间,可以单独定义集合 bean,并通过 property 标签的 ref 属性将定义好的集合 bean 赋值给需要的 bean。由于集合对象不能通过 setter 方法和构造方法添加元素,因此,不能通过如下方式给集合对象添加元素:
<bean id="list" class="java.util.ArrayList">
<property name=""></property>
<constructor-arg></constructor-arg>
</bean>
首先引入 util 命名空间,如下:
applicationContext.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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<bean id="student" class="com.test.Student">
<property name="sname" value="小王"></property>
<property name="teachers" ref="t">
</property>
</bean>
<util:list id="t">
<value>A</value>
<value>B</value>
</util:list>
</beans>
2.2 List 元素为自定义引用类型
若 Student 类中 teachers 属性为自定义引用类型,即 Teacher 类,如下:
Teacher.java
public class Teacher {
private String tname;
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
@Override
public String toString() {
return "Teacher [tname=" + tname + "]";
}
}
applicationContext.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.xsd">
<bean id="student" class="com.test.Student">
<property name="sname" value="小李"></property>
<property name="teachers">
<list>
<ref bean="t1"/>
<ref bean="t2"/>
</list>
</property>
</bean>
<bean id="t1" class="com.test.Teacher">
<property name="tname" value="陈老师"></property>
</bean>
<bean id="t2" class="com.test.Teacher">
<property name="tname" value="刘老师"></property>
</bean>
</beans>
运行结果:
Student [sname=小李, teachers=[Teacher [tname=陈老师], Teacher [tname=刘老师]]]
使用 util 命名空
applicationContext.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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<bean id="student" class="com.test.Student">
<property name="sname" value="小李"></property>
<property name="teachers" ref="t">
</property>
</bean>
<util:list id="t">
<ref bean="t1"/>
<ref bean="t2"/>
</util:list>
<bean id="t1" class="com.test.Teacher">
<property name="tname" value="陈老师"></property>
</bean>
<bean id="t2" class="com.test.Teacher">
<property name="tname" value="刘老师"></property>
</bean>
</beans>
3 Map 属性注入方法
需要注入属性的对象为 Student 类,如下:
Student.java
package com.test;
import java.util.Map;
public class Student {
private String sname;
private Map teachers;
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Map getTeachers() {
return teachers;
}
public void setTeachers(Map teachers) {
this.teachers = teachers;
}
@Override
public String toString() {
return "Student [sname=" + sname + ", teachers=" + teachers + "]";
}
}
applicationContext.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.xsd">
<bean id="student" class="com.test.Student">
<property name="sname" value="小王"></property>
<property name="teachers">
<map>
<entry>
<key><value>1001</value></key>
<value>程老师</value>
</entry>
<entry>
<key><value>1002</value></key>
<value>杨老师</value>
</entry>
</map>
</property>
</bean>
</beans>
运行结果:
Student [sname=小王, teachers={1001=程老师, 1002=杨老师}]
使用 util 命名空
applicationContext.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"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<bean id="student" class="com.test.Student">
<property name="sname" value="小李"></property>
<property name="teachers" ref="t">
</property>
</bean>
<util:map id="t">
<entry>
<key><value>1001</value></key>
<value>程老师</value>
</entry>
<entry>
<key><value>1002</value></key>
<value>杨老师</value>
</entry>
</util:map>
</beans>
相关文章
- 面试(4)-spring-Spring面试题和答案
- Spring_2_Spring中lazy-init和scope属性
- Spring Bean的生命周期
- Spring Boot 的 HTTP 客户端框架
- Spring 注解玩大全
- Spring Boot 2.x基础教程:JSR-303实现请求参数校验
- 《Spring攻略(第2版)》——1.9 用依赖检查属性
- Spring中注入List,Set,Map,Properties的xml文件配置方法
- 关于Spring集成Quartz的concurrent属性
- 2015第29周六Spring
- Spring Boot的属性配置
- spring aop使用,spring aop注解,Spring切面编程
- spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务
- spring的bean标签属性
- spring框架漏洞整理(Spring Data漏洞)
- 【Spring常见错误】Initialization failed for ‘https://start.spring.io‘
- 解决Spring Boot 使用RedisTemplate 存储键值出现乱码 xacxedx00x05tx00
- Spring之注入复杂类型属性
- 从头认识Spring-1.7 如何通过属性注入Bean?(1)-如何通过属性向对象注入值?
- 整合SSM时报错:java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer;
- spring IOC(Spring 生命周期,先1.构造方式,2,初始化方法,3,目标方法,4,销毁方法)