Spring Bean的作用域

本文详细解析了Spring框架中Bean的五种作用域:singleton、prototype、request、session及globalsession,通过具体代码示例展示了不同作用域下Bean实例的行为特征。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Bean的作用域

  • singleton:单例,指一个Bean容器中只存在一份。
  • prototype:每次请求(每次使用)创建新的实例,destroy方式不生效
  • request:每次http请求创建一个实例,且仅在当前request内有效
  • session:同上,每次http请求创建,当前session内有效
  • global session:基于portlet的web中有效(portlet定义global session),如果在web中,同session

 对singletonprototype两个作用域进行测试,其他作用域选项可以参照类似的测试过程:

准备工作包含155620_jgVV_3436951.png

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容器获取都是新的实例。

转载于:https://my.oschina.net/pioneerDev/blog/886105

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值