spring中bean属性scope介绍

spring中bean属性scope介绍

singleton:SpringIoc容器只会创建该Bean的唯一实例,所有的请求和引用都只使用这个实例
Property: 每次请求都创建一个实例
request:    在一次Http请求中,容器会返回该Bean的同一个实例,而对于不同的用户请求,会返回不同的实例。需要注意的是,该作用域仅在基于Web的Spring ApplicationContext情形下有效,以下的session和global Session也是如此
session:同上,唯一的区别是请求的作用域变为了session
global session:全局的HttpSession中,容器会返回该bean的同一个实例,典型为在是使用portlet context的时候有效(这个概念本人也不懂)

注意:如果要用到request,session,global session时需要配置

servlet2.4及以上:
在web.xml中添加:
<listener>
    <listener-class>org.springframework.web.context.scope.RequestContextListener />
</listener>

servlet2.4以下:
需要配置一个过滤器
<filter>
    <filter-name>XXXX</filter-name>
    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
<filter-mapping>
    <filter-name>XXXX</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

另外,从2.0开始,可以自己定义作用域,但需要实现scope,并重写get和remove方法

特别要引起注意的是:
   一般情况下前面两种作用域是够用的,但如果有这样一种情况:singleton类型的bean引用一个prototype的bean时会出现问题,因为singleton只初始化一次,但prototype每请求一次都会有一个新的对象,但prototype类型的bean是singleton类型bean的一个属性,理所当然不可能有新prototpye的bean产生,与我们的要求不符

解决方法:
1.放弃Ioc,这与设计初衷不符,并代码间会有耦合
2,Lookup方法注入,推荐

但在用Lookup方法注入时也需要注意一点:需要在引用的Bean中定一个一个抽象地返回被引用对象的方法

package com.huyong.lookup;

import java.util.Calendar;

/**
* @author  
*/
public class CurrentTime {
private Calendar now = Calendar.getInstance();

public void printCurrentTime() {
System.out.println("Current Time:" + now.getTime());
}

}


package com.huyong.lookup;

/**
* @author HuYong 
*/
public abstract class LookupBean {
private CurrentTime currentTime;

public CurrentTime getCurrentTime() {
return currentTime;
}

public void setCurrentTime(CurrentTime currentTime) {
this.currentTime = currentTime;

}
public abstract CurrentTime createCurrentTime();

}

<?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.0.xsd">

<bean id="currentTime" class="com.huyong.lookup.CurrentTime"
scope="prototype">
</bean>
<bean id="lookupBean" class="com.huyong.lookup.LookupBean"
scope="singleton">
<lookup-method name="createCurrentTime" bean="currentTime" />
<property name="currentTime" ref="currentTime"></property>
</bean>

</beans>


Main Test:
package com.huyong.lookup;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

/**
* @author HuYong Email:yate7571@hotmail.com
*/
public class LookupMain {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ClassPathResource resource = new ClassPathResource(
"applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);

LookupBean lookupBean = (LookupBean) factory.getBean("lookupBean");
System.out.println("----------first time---------");
System.out.println("getCurrentTime:");
lookupBean.getCurrentTime().printCurrentTime();
System.out.println("createCurrentTime:");
lookupBean.createCurrentTime().printCurrentTime();

Thread.sleep(12345);

System.out.println("---------second time---------");
System.out.println("getCurrentTime:");
LookupBean lookupBean02 = (LookupBean) factory.getBean("lookupBean");
lookupBean02.getCurrentTime().printCurrentTime();
System.out.println("createCurrentTime:");
lookupBean02.createCurrentTime().printCurrentTime();

}

}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值