`

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();
	}
}

 

 

分享到:
评论

相关推荐

    spring bean 属性总结

    在Spring中,核心概念之一就是Bean,它是一个简单的Java对象,由Spring IoC容器管理。Spring通过XML配置文件或注解来定义、配置和管理Beans。下面将深入探讨`&lt;beans&gt;`、`&lt;bean&gt;`及其属性,以及其他相关的配置元素。 ...

    Spring官方文档之核心篇

    在Bean的配置中,可以注入各种类型的值,包括基本数据类型、其他Bean的引用、内部Bean、集合类型等。 - **直接值**:可以注入基本类型和字符串等。 - **引用其他Bean**:可以将一个Bean作为依赖注入到另一个Bean中...

    spring学习资源共享

    本资源集合题为“spring学习资源共享”,旨在为学习者提供一套全面的Spring教程资料,助力大家提升技能,实现职业发展。 首先,Spring是一个开源的Java平台,它为构建应用程序提供了全面的框架支持。Spring的核心...

    Spring Boot Redis Session 共享

    本篇将深入探讨如何在Spring Boot项目中利用Redis进行Session共享。 首先,我们需要理解Spring Boot和Redis的基础。Spring Boot是Spring框架的一个轻量级衍生品,它简化了创建独立的、生产级别的基于Spring的应用...

    SSHnote_Spring基本配置

    公用的集合bean(Common Collection Bean)是指可以被多个bean共享的集合对象。你可以在配置文件中定义一个bean,然后在其他bean中引用它,以避免重复创建相同的集合对象。例如,定义一个全局的`&lt;bean id="common...

    Spring Reference Core Technologies

    单例作用域是最常用的作用域之一,意味着在Spring IoC容器中只存在一个Bean实例。 - **1.5.2 原型作用域** 原型作用域意味着每次请求都会创建一个新的Bean实例。 - **1.5.3 单例Bean与原型Bean的依赖** 当...

    Spring的经典面试题

    其中`org.springframework.beans`主要提供Bean操作的基本支持,而`org.springframework.context`则在此基础上添加了更丰富的功能,如国际化支持、资源加载等。 - **BeanFactory**:是最基本的IoC容器,提供最简单...

    spring-core.pdf

    - **1.6.1 生命周期回调**: Spring支持在Bean的生命周期中执行自定义的方法,包括初始化和销毁方法。 - **初始化回调**: 通过`InitializingBean`接口或者在Bean定义中指定`init-method`属性来定义初始化方法。 - *...

    Spring-Reference_zh_CN(Spring中文参考手册)

    9.5.4. 为不同的bean配置不同的事务语义 9.5.5. &lt;tx:advice/&gt; 有关的设置 9.5.6. 使用 @Transactional 9.5.6.1. @Transactional 有关的设置 9.5.7. 插入事务操作 9.5.8. 结合AspectJ使用 @Transactional 9.6. 编程式...

    Spring开发笔记

    - **集合注入**:Spring支持list、map等集合类型的注入,以及引用其他bean对象。 6. **Bean的作用域**: - **Singleton**:默认作用域,Spring容器中只会有一个共享bean实例,所有请求都将返回同一个实例。 - **...

    Spring中的参数注入.pdf

    这种方式允许在多个Bean之间共享同一个Bean对象。注入外部Bean的示例如下: ```xml &lt;bean id="studentInfoServiceObject" class="com.ohmygod.service.StudentInfoService"&gt; &lt;/bean&gt; &lt;bean id=...

    spring学习小结

    Spring 提供了一种方式来共享和重用bean的属性,通过定义抽象bean(`abstract="true"`),然后让其他bean继承这些属性。这种方式减少了重复的配置,提高代码复用。 示例: ```xml &lt;bean id="AbstractBean" ...

    spring aop的jar包

    4. **org.springframework.core-3.0.5.RELEASE.jar**:这是Spring的核心库,包含了基本的类型转换、反射、集合操作等基础工具类,是Spring框架的基石。 5. **org.springframework.aop-3.0.5.RELEASE.jar**:这个...

    SSM项目集成shiro搭建session共享

    通过这种方式,即使用户在不同服务器之间跳转,也能保持登录状态。 总之,这个项目通过合理的技术选型和集成,实现了基于SSM的Web应用的安全控制和session共享,提高了系统的可扩展性和用户体验。在实际开发中,还...

    38. Spring Boot分布式Session状态保存Redis【从零开始学Spring Boot】

    在Spring Boot应用中,随着系统复杂度的增加,单一服务器往往无法满足高并发、高可用的需求,因此我们会采用分布式架构。然而,在分布式环境下,传统的基于HTTP Session的状态管理方式会遇到问题,因为每个服务器都...

    小米_spring1

    在Spring框架中,自动装配(Auto-Wiring)是一种便捷的管理Bean依赖关系的方法,它可以帮助开发者减少显式配置。Spring提供了多种自动装配的方式: 1. `no`:默认选项,不进行任何自动装配,所有的依赖都需要手动...

    spring4框架系列 [ 4 ]

    在Spring4框架系列的第四部分,我们将深入探讨如何利用XML配置进行依赖注入,以及与之相关的各种特性。依赖注入是Spring框架的核心特性,它有助于实现松耦合和更好的可测试性。下面,我们将详细讲解以下几个关键知识...

    Spring项目初学 (二)

    此外,Spring的依赖注入(Dependency Injection,DI)允许我们声明bean之间的依赖关系,而无需在代码中硬编码这些关系。这可以通过XML配置、注解或Java配置来实现。在项目中,可能会看到使用@Autowired注解来自动...

    SSH笔记-Spring整合Struts2

    - **重用**:Spring的Bean可以在多个Action之间共享,减少代码重复。 总的来说,Spring整合Struts2是一种最佳实践,能够提升企业级应用的开发效率和质量。理解这一整合过程,对于任何想要深入理解和使用SSH框架的...

    Spring面试专题.pdf

    Spring提供不同的生命周期回调方法,如初始化时的init()方法和销毁前的destroy()方法,以及通过@Bean注解的scope属性定义bean的作用域(单例、原型、请求、会话等)。 Spring inner beans是指在另一个bean定义内部...

Global site tag (gtag.js) - Google Analytics