使用Spring(七)集合合并、bean的作用域(prototype,singleton)

1.applicationContext.xml

作用域描述

singleton

在每个Spring IoC容器中一个bean定义对应一个对象实例。

prototype

一个bean定义对应多个对象实例。



<?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="parent" abstract="true" class="com.yw.test05.ComplexObject">
		<property name="adminEmails">
			<props>
				<prop key="administrator">administrator@somecompany.com</prop>
				<prop key="support">support@somecompany.com</prop>
			</props>
		</property>


	</bean>
	<bean id="child" parent="parent">
		<property name="adminEmails">
			<!-- the merge is specified on the *child* collection definition -->
			<props merge="true">
				<prop key="sales">sales@somecompany.com</prop>
				<prop key="support">support@somecompany.co.uk</prop>
			</props>
		</property>
	</bean>


	<bean id="obj" class="java.lang.Object" scope="prototype" ></bean>
	<bean id="obj2" class="java.lang.Object" scope="singleton" ></bean>
</beans>

2.

package com.yw.test05;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class ComplexObject
{
	private Properties adminEmails;
	private List someList;
	private Map someMap;
	private Set someSet;
	public Properties getAdminEmails()
	{
		return adminEmails;
	}
	public void setAdminEmails(Properties adminEmails)
	{
		this.adminEmails = adminEmails;
	}
	public List getSomeList()
	{
		return someList;
	}
	public void setSomeList(List someList)
	{
		this.someList = someList;
	}
	public Map getSomeMap()
	{
		return someMap;
	}
	public void setSomeMap(Map someMap)
	{
		this.someMap = someMap;
	}
	public Set getSomeSet()
	{
		return someSet;
	}
	public void setSomeSet(Set someSet)
	{
		this.someSet = someSet;
	}
	@Override
	public String toString()
	{
		return "ComplexObject [adminEmails=\n" + adminEmails + ",\nsomeList=\n" + someList + ", \nsomeMap=\n" + someMap
				+ ", \nsomeSet=\n" + someSet + "]";
	}
	
}

3.

package com.yw.test05;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Test03
{
	public static void main(String[] args)
	{

		// 实例化容器方法三classpath:
		// ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {
		// "com/yw/test05/applicationContext.xml"});
		ApplicationContext context = new FileSystemXmlApplicationContext(
				new String[] { "classpath:com/yw/test05/applicationContext3.xml" });
		// of course, an ApplicationContext is just a BeanFactory
		BeanFactory factory = (BeanFactory) context;

		// 集合合并
		Object obj = factory.getBean("child");
		System.out.println("obj=" + obj);

		//prototype
		Object obj2 = factory.getBean("obj");
		System.out.println("prototype obj2=" + obj2);

		Object obj3 = factory.getBean("obj");
		System.out.println("prototype obj3=" + obj3);
		
		//singleton
		Object obj4 = factory.getBean("obj2");
		System.out.println("singleton obj4=" + obj4);

		Object obj5 = factory.getBean("obj2");
		System.out.println("singleton obj5=" + obj5);
	}
}

4.运行


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值