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.运行