Spring之在不同 Bean之间共享集合

  

使用基本的集合标签定义集合时, 不能将集合作为独立的 Bean 定义, 导致其他 Bean 无法引用该集合,所以无法在不同 Bean 之间共享集合。

可以使用 util schema 里的集合标签定义独立的集合 Bean, 需要注意的是,,必须在 <beans> 根元素里添加 util schema 定义。

 

 示例:

 

1. 添加模型类

 

package xyz.huning.spring4.di.xml.beancfg.sharecollection;

public class Student {

	private int id;
	
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + "]";
	}
	
}

 

package xyz.huning.spring4.di.xml.beancfg.sharecollection;

import java.util.List;

public class School {

	private int id;
	
	private String name;
	
	private List<Student> students;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Student> getStudents() {
		return students;
	}

	public void setStudents(List<Student> students) {
		this.students = students;
	}

	@Override
	public String toString() {
		return "School [id=" + id + ", name=" + name + ", students=" + students
				+ "]";
	}
	
}

 

2. 添加配置

 

<?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="student1" class="xyz.huning.spring4.di.xml.beancfg.sharecollection.Student">
		<property name="id" value="1"></property>
		<property name="name" value="Tom"></property>
	</bean>
	
	<bean id="student2" class="xyz.huning.spring4.di.xml.beancfg.sharecollection.Student">
		<property name="id" value="2"></property>
		<property name="name" value="Kat"></property>
	</bean>
	
	<!--配置单例的bean,以供多个bean进行引用,需要导入util命名空间-->
	<util:list id="students1">
		<ref bean="student1"/>
        <ref bean="student2"/>
	</util:list>
	
	
	<bean id="school1" class="xyz.huning.spring4.di.xml.beancfg.sharecollection.School">
		<property name="id" value="1"></property>
		<property name="name" value="StarSchool"></property>
		<property name="students" ref="students1"></property>
	</bean>
	
	<bean id="school2" class="xyz.huning.spring4.di.xml.beancfg.sharecollection.School">
		<property name="id" value="2"></property>
		<property name="name" value="SunSchool"></property>
		<property name="students" ref="students1"></property>
	</bean>
	
</beans>

 

 

3. 添加测试类:

 

package xyz.huning.spring4.di.xml.beancfg.sharecollection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

	public static void main(String[] args) {
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("sharecollection.xml");
		
		School school1 = ctx.getBean("school1", School.class);
		System.out.println(school1);
		
		School school2 = ctx.getBean("school2", School.class);
		System.out.println(school2);
		
		((ClassPathXmlApplicationContext)ctx).close();
	}
}

 

 

猜你喜欢

转载自ihuning.iteye.com/blog/2224173