Bean的作用域
- singleton:单例,指一个Bean容器中只存在一份。
- prototype:每次请求(每次使用)创建新的实例,destroy方式不生效
- request:每次http请求创建一个实例,且仅在当前request内有效
- session:同上,每次http请求创建,当前session内有效
- global session:基于portlet的web中有效(portlet定义global session),如果在web中,同session
对singleton和prototype两个作用域进行测试,其他作用域选项可以参照类似的测试过程:
准备工作包含
1、BeanScope.java 测试类
package com.abc;
public class BeanScope {
/**
* 输出对象的hashCode
*/
public void printHashCode(){
System.out.println("BeanScope: " + this.hashCode());
}
}
2、Spring-BeanScope.xml SpringBean配置文件
- singleton配置方式(prototype作用域配置:把scope值改为prototype即可)
<?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.abc.BeanScope" scope="singleton"></bean>
</beans>
3、单元测试类
package com.abc.test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.abc.BeanScope;
@RunWith(BlockJUnit4ClassRunner.class)
public class BeanScopeTest {
private ClassPathXmlApplicationContext context = null;
@Before
public void init(){
context = new ClassPathXmlApplicationContext("classpath:Spring-BeanScope.xml");
context.start();
}
@After
public void destory(){
context.destroy();
}
@Test
public void testSingleton(){
BeanScope scope1 = (BeanScope) context.getBean("beanScope");
scope1.printHashCode();
BeanScope scope2 = (BeanScope) context.getBean("beanScope");
scope2.printHashCode();
}
}
4、测试结果
- singleton测试结果(singleton为默认选项)
四月 24, 2017 4:02:25 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@182decdb: startup date [Mon Apr 24 16:02:25 HKT 2017]; root of context hierarchy
四月 24, 2017 4:02:25 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [Spring-BeanScope.xml]
BeanScope: 1845904670
BeanScope: 1845904670
四月 24, 2017 4:02:25 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@182decdb: startup date [Mon Apr 24 16:02:25 HKT 2017]; root of context hierarchy
- prototype测试结果
四月 24, 2017 4:06:41 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@182decdb: startup date [Mon Apr 24 16:06:41 HKT 2017]; root of context hierarchy
四月 24, 2017 4:06:42 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [Spring-BeanScope.xml]
BeanScope: 1596000437
BeanScope: 832947102
四月 24, 2017 4:06:42 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@182decdb: startup date [Mon Apr 24 16:06:41 HKT 2017]; root of context hierarchy
5、结论
- singleton在同一个Spring Bean容器中,实例是唯一的。
- prototype每次在Spring Bean容器获取都是新的实例。