首先是一张图
接着创建一个类
package com.imooc.bean;
public class BeanScope {
public void say() {
//this指的是这个类。hashcode用来区分实例
System.out.println("BeanScope say : " + this.hashCode());
}
}
创建一个xml配置文件,设置scope为单例
<?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="beanScope" class="com.imooc.bean.BeanScope" scope="singleton"></bean>
</beans>
测试用例
package com.imooc.test.bean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.imooc.bean.BeanScope;
import com.imooc.test.base.UnitTestBase;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScope extends UnitTestBase {
public TestBeanScope() {
super("classpath*:spring-beanscope.xml");
}
@Test
//创建两个实例,从同一个bean容器中获取
public void testSay() {
BeanScope beanScope = super.getBean("beanScope");
beanScope.say();
BeanScope beanScope2 = super.getBean("beanScope");
beanScope2.say();
}
}
结果:
8月 01, 2018 10:58:42 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Wed Aug 01 10:58:42 CST 2018]; root of context hierarchy
8月 01, 2018 10:58:42 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from URL [file:/E:/eclipse/workspace/Spring/classes/spring-beanscope.xml]
BeanScope say : 1014486152
BeanScope say : 1014486152
8月 01, 2018 10:58:43 上午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Wed Aug 01 10:58:42 CST 2018]; root of context hierarchy
可以看出两个bean的hashcode是一样的,也就证明了两次请求返回的bean是同一个。
那么如果设置两个单元测试呢?
package com.imooc.test.bean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.imooc.bean.BeanScope;
import com.imooc.test.base.UnitTestBase;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScope extends UnitTestBase {
public TestBeanScope() {
super("classpath*:spring-beanscope.xml");
}
@Test
public void testSay() {
BeanScope beanScope = super.getBean("beanScope");
beanScope.say();
}
@Test
public void testSay2() {
BeanScope beanScope = super.getBean("beanScope");
beanScope.say();
}
}
测试结果为:
8月 01, 2018 11:08:24 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@442675e1: startup date [Wed Aug 01 11:08:24 CST 2018]; root of context hierarchy
8月 01, 2018 11:08:24 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from URL [file:/E:/eclipse/workspace/Spring/classes/spring-beanscope.xml]
BeanScope say : 20853837
8月 01, 2018 11:08:24 上午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@442675e1: startup date [Wed Aug 01 11:08:24 CST 2018]; root of context hierarchy
8月 01, 2018 11:08:24 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1a942c18: startup date [Wed Aug 01 11:08:24 CST 2018]; root of context hierarchy
8月 01, 2018 11:08:24 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from URL [file:/E:/eclipse/workspace/Spring/classes/spring-beanscope.xml]
BeanScope say : 1991371192
8月 01, 2018 11:08:24 上午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1a942c18: startup date [Wed Aug 01 11:08:24 CST 2018]; root of context hierarchy
可以看到,两个hashcode不一样了,为什么呢?
因为在父类UnitTestBase中规定了,在每一个单元测试执行前都会创建容器,但是在执行完毕后会销毁对应容器。所以两次的bean容器实际上是不一样的,导致两次的hashcode不一样。
同理,测试prototype,首先改为同一个容器
package com.imooc.test.bean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.imooc.bean.BeanScope;
import com.imooc.test.base.UnitTestBase;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScope extends UnitTestBase {
public TestBeanScope() {
super("classpath*:spring-beanscope.xml");
}
@Test
public void testSay() {
BeanScope beanScope = super.getBean("beanScope");
beanScope.say();
BeanScope beanScope2 = super.getBean("beanScope");
beanScope2.say();
}
}
测试结果:
8月 01, 2018 11:14:44 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Wed Aug 01 11:14:44 CST 2018]; root of context hierarchy
8月 01, 2018 11:14:44 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from URL [file:/E:/eclipse/workspace/Spring/classes/spring-beanscope.xml]
BeanScope say : 477533894
BeanScope say : 1611241809
8月 01, 2018 11:14:45 上午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Wed Aug 01 11:14:44 CST 2018]; root of context hierarchy
可以看到即使是同一个容器的同一个类,每次调用都会创建新的实例。
其他的后续自己尝试。