Bean的作用域介绍(Singleton与prototype)

上传人:油条 文档编号:11918982 上传时间:2017-10-15 格式:DOC 页数:4 大小:24.50KB
返回 下载 相关 举报
Bean的作用域介绍(Singleton与prototype)_第1页
第1页 / 共4页
Bean的作用域介绍(Singleton与prototype)_第2页
第2页 / 共4页
Bean的作用域介绍(Singleton与prototype)_第3页
第3页 / 共4页
Bean的作用域介绍(Singleton与prototype)_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《Bean的作用域介绍(Singleton与prototype)》由会员分享,可在线阅读,更多相关《Bean的作用域介绍(Singleton与prototype)(4页珍藏版)》请在金锄头文库上搜索。

1、Bean 的作用域介绍( Singleton 与 prototype)Spring Bean 作用域介绍:singleton:Spring Ioc 容器只会创建该 Bean 的唯一实例,所有的请求和引用都只使用这个实例Property: 每次请求都创建一个新实例request: 在一次 Http 请求中,容器会返回该 Bean 的同一个实例,而对于不同的用户请求,会返回不同的实例。需要注意的是,该作用域仅在基于 Web 的 Spring ApplicationContext 情形下有效,以下的session 和 global Session 也是如此session:同上,唯一的区别是请求的作用

2、域变为了 sessionglobal session:全局的 HttpSession 中,容器会返回该 bean 的同一个实例,典型为在是使用 portlet context 的时候有效(这个概念本人也不懂)注意:如果要用到 request,session,global session 时需要配置servlet2.4及以上:在 web.xml 中添加:org.springframework.web.context.scope.RequestContextListener /servlet2.4以下:需要配置一个过滤器XXXXorg.springframework.web.filter.Requ

3、estContextFilterXXXX/*另外,从2.0开始,可以自己定义作用域,但需要实现 scope,并重写 get 和 remove 方法特别要引起注意的是:一般情况下前面两种作用域是够用的,但如果有这样一种情况:singleton 类型的 bean 引用一个prototype 的 bean 时会出现问题,因为 singleton 只初始化一次,但 prototype 每请求一次都会有一个新的对象,但 prototype 类型的 bean 是 singleton 类型 bean 的一个属性,理所当然不可能有新 prototpye的 bean 产生,与我们的要求不符解决方法:1.放弃 I

4、oc,这与设计初衷不符,并代码间会有耦合2,Lookup 方法注入,推荐但在用 Lookup 方法注入时也需要注意一点:需要在引用的 Bean 中定一个一个抽象地返回被引用对象的方法package com.huyong.lookup;import java.util.Calendar;/* author HuYong Email:*/public class CurrentTime private Calendar now = Calendar.getInstance();public void printCurrentTime() System.out.println(Current Tim

5、e: + now.getTime();package com.huyong.lookup;/* author HuYong Email:*/public abstract class LookupBean private CurrentTime currentTime;public CurrentTime getCurrentTime() return currentTime;public void setCurrentTime(CurrentTime currentTime) this.currentTime = currentTime;public abstract CurrentTime

6、 createCurrentTime();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:*/public class LookupMain /* param args* throws Except

7、ion*/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.printl

8、n(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();感觉 Spring 的东西比较杂,学好 spring 一定要明白反射和代理是怎么回事!渐渐的也挺会到了 Spring 的好处!简单就是美!

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号