zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【spring框架】集合属性注入方法

2023-09-11 14:20:52 时间

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>