【spring框架】bean的生命范围(scope)

<bean ...></bean>中有一个属性,叫scope,就是设定生命范围的。
其中有这么几个属性:
表 3.4. Bean作用域

作用域 描述 
singleton 
 在每个Spring IoC容器中一个bean定义对应一个对象实例。
 不论你拿多少个bean,都只是一个对象,叫"单例"
 
prototype 
 一个bean定义对应多个对象实例。
 prototype叫"原型",就是谁要原型,我给它一个新的,每一次生成一个新的对象。
request 
 在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例, 它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。
 
session 
 在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。
 
global session 
 在一个全局的HTTP Session中,一个bean定义对应一个实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅在基于web的Spring ApplicationContext情形下有效。
 
最常用的是singleton和prototype ,默认是singleton。


测试singleton:
beans.xml配置:
<?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-2.5.xsd">


  <bean id="u" class="cn.edu.hpu.dao.Impl.UserDaoImpl">
  	<property name="daoId" value="8"></property>
  	<property name="daoStatus" value="good"></property>
  </bean>
	
  <bean id="userService" class="cn.edu.hpu.service.UserService" scope="singleton"><!--或默认不写-->
  	 <constructor-arg>
  	 	<ref bean="u"/>
  	 </constructor-arg>
  </bean>
  
</beans>

测试:
@Test
public void testAdd() throws Exception{
	BeanFactory ctx=new ClassPathXmlApplicationContext("beans.xml");
	
	UserService userService=(UserService)ctx.getBean("userService");
	UserService userService2=(UserService)ctx.getBean("userService");
	System.out.println(userService==userService2);
}
结果打印了:true。说明确实使用的是一个对象


测试prototype:
beans.xml配置:
<?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-2.5.xsd">


  <bean id="u" class="cn.edu.hpu.dao.Impl.UserDaoImpl">
  	<property name="daoId" value="8"></property>
  	<property name="daoStatus" value="good"></property>
  </bean>
	
  <bean id="userService" class="cn.edu.hpu.service.UserService" scope="prototype">
  	 <constructor-arg>
  	 	<ref bean="u"/>
  	 </constructor-arg>
  </bean>
</beans>

测试:
@Test
public void testAdd() throws Exception{
	BeanFactory ctx=new ClassPathXmlApplicationContext("beans.xml");
	
	UserService userService=(UserService)ctx.getBean("userService");
	UserService userService2=(UserService)ctx.getBean("userService");
	System.out.println(userService==userService2);
}
结果打印了:false。说明使用的不是一个对象


一般开发时选择prototype来使用。

转载请注明出处:http://blog.csdn.net/acmman

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

光仔December

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值