java Spring bean作用域
1. Singleton作用域
当一个bean的作用域为singleton, 那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。
换言之,当把一个bean定义设置为singlton作用域时,Spring IoC容器只会创建该bean定义的唯一实例。这个单一实例会被存储到单例缓存(singleton cache)中,并且所有针对该bean的后续请求和引用都将返回被缓存的对象实例。
2. Prototype作用域
Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的bean实例。根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。
下面几个作用域是web里面的,需要添加配置
1. 初始化web配置
<web-app>
...
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>
2. Request作用域
<bean id="loginAction" class="com.foo.LoginAction" scope="request"/>
针对每次HTTP请求,Spring容器会根据loginAction bean定义创建一个全新的LoginAction bean实例, 且该loginAction bean实例仅在当前HTTP request内有效,因此可以根据需要放心的更改所建实例的内部状态, 而其他请求中根据loginAction bean定义创建的实例,将不会看到这些特定于某个请求的状态变化。 当处理请求结束,request作用域的bean实例将被销毁。
3. Session作用域
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>
针对某个HTTP Session,Spring容器会根据userPreferences bean定义创建一个全新的userPreferences bean实例, 且该userPreferences bean仅在当前HTTP Session内有效。 与request作用域一样,你可以根据需要放心的更改所创建实例的内部状态,而别的HTTP Session中根据userPreferences创建的实例, 将不会看到这些特定于某个HTTP Session的状态变化。 当HTTP Session最终被废弃的时候,在该HTTP Session作用域内的bean也会被废弃掉。
4. global session作用域
<bean id="userPreferences" class="com.foo.UserPreferences" scope="globalSession"/>
global session作用域类似于标准的HTTP Session作用域,不过它仅仅在基于portlet的web应用中才有意义。Portlet规范定义了全局Session的概念,它被所有构成某个portlet web应用的各种不同的portlet所共享。在global session作用域中定义的bean被限定于全局portlet Session的生命周期范围内。
请注意,假如你在编写一个标准的基于Servlet的web应用,并且定义了一个或多个具有global session作用域的bean,系统会使用标准的HTTP Session作用域,并且不会引起任何错误。
4. 如果要将一个不同作用域的bean注入到另一个作用域的bean中,那么就需要用到代理注入
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session">
<!-- this next element effects the proxying of the surrounding bean -->
<aop:scoped-proxy/>
</bean>
<!-- a singleton-scoped bean injected with a proxy to the above bean -->
<bean id="userService" class="com.foo.SimpleUserService">
<!-- a reference to the proxied
'userPreferences' bean -->
<property name="userPreferences" ref="userPreferences"/>
</bean>
注意:<aop:scoped-proxy/> 不能和作用域为singleton或prototype的bean一起使用。为singleton bean创建一个scoped proxy将抛出BeanCreationException异常。
自定义作用域:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="thread">
<bean class="com.foo.ThreadScope"/>
</entry>
</map>
</property>
</bean> <bean id="bar" class="x.y.Bar" scope="thread">
<property name="name" value="Rick"/>
<aop:scoped-proxy/>
</bean> <bean id="foo" class="x.y.Foo">
<property name="bar" ref="bar"/>
</bean> </beans>
java Spring bean作用域的更多相关文章
- Spring 中 ApplicationContext 和 BeanFactory 的区别,以及 Spring bean 作用域
//从ApplicationContext 中取 bean ApplicationContext ac = new ClassPathXmlApplicationContext ( "com ...
- 【Spring】IoC容器 - Spring Bean作用域Scope(含SpringCloud中的RefreshScope )
前言 上一章学习了[依赖来源],本章主要讨论SpringBean的作用域,我们这里讨论的Bean的作用域,很大程度都是默认只讨论依赖来源为[Spring BeanDefinition]的作用域,因为在 ...
- [spring] -- bean作用域跟生命周期篇
作用域 singleton : 唯一 bean 实例,Spring 中的 bean 默认都是单例的. prototype : 每次请求都会创建一个新的 bean 实例. request : 每一次HT ...
- Spring bean作用域
全当知识要点记录了,大家随意踩踩. spring的作用域有以下几种singleton作用域prototype作用域request作用域session作用域global-session作用域 1. si ...
- Spring Bean作用域实例
在Spring中,bean作用域用于确定哪种类型的 bean 实例应该从Spring容器中返回给调用者.bean支持的5种范围域: 单例 - 每个Spring IoC 容器返回一个bean实例 原型- ...
- 第31章 Spring bean 作用域
每日一句 I must say a word about fear. It is life's only true opponent. Only fear can defeat life. 这里必须说 ...
- Spring Bean 作用域
Bean 的作用域 当在 Spring 中定义一个 bean 时,你必须声明该 bean 的作用域的选项.例如,为了强制 Spring 在每次需要时都产生一个新的 bean 实例,你应该声明 bean ...
- Java Spring Bean相关配置
1.Bean配置信息组成部分: (1)Bean实现类 (2)Bean的属性信息 (3)Bean的依赖关系 (4)Bean的行为配置 2.配置方式: (1)XML配置 (2)注解配置 (3)Java类配 ...
- java spring bean的什么周期
http://www.cnblogs.com/TIMHY/p/7794973.html
随机推荐
- grb文件的读取
grb文件的读取(转自:http://blog.sciencenet.cn/blog-922140-713837.html) read_grib.r4.rar 今天来斟酌了下grb文件格式的读取,现在 ...
- MVC 自定义AuthorizeAttribute实现权限管理
在上一节中提到可以使用AuthorizeAttribute进行权限管理: [Authorize] public ActionResult TestAuthorize() { return View() ...
- How to Set Word Document Properties with C#
Word properties shows a brief description about one document. Through properties, we can learn gener ...
- GIS数据格式:Shapefile
转自:http://lab.osgeo.cn/2449.html Shapefile是ESRI提出的数据格式,随着ArcView GIS 3.x发布,属于简单要素类.Shapefile由于其数据结构简 ...
- STL总结之functor
STL中仿函数是重要的组成部分.所谓的仿函数就是通过重载括号运算符实现的, 如下: STL库中都是泛型仿函数如小于操作: STL中定义了许多有用的操作,如less(小于), less_equal(小于 ...
- nmon for linux
nmon(为Nigel's performance Monitor的简写) for linux工具是 IBM开源的在POWER, x86, x86_64, Mainframe & now AR ...
- mac 卸载 XCode
http://blog.csdn.net/songques/article/details/7244144
- office文件密码破解方法及软件
今天会用到3个软件 1.Office Password Remover 说明:这个软件可以很快破解.doc .xls的密码 使用方法:参考百度经验里面的文章http://jingyan.baidu ...
- Hadoop中java.lang.ClassCastException: partition解决方法
java.lang.ClassCastException: partition.KpiWritable cannot be cast to org.apache.hadoop.io.LongWrita ...
- hadoop入门必备基础知识
1.对Linux 系统的要求 会基本的命令: (1)知道root用户 (2)ls命令会查看文件夹内容 (3)cd命令等2.Java 的要求 ...