在Spring中,bean作用域用于确定哪种类型的 bean 实例应该从Spring容器中返回给调用者。bean支持的5种范围域:
  1. 单例(singleton) - 每个Spring IoC 容器返回一个bean实例(默认)
  2. 原型(prototype)- 当每次请求时返回一个新的bean实例
  3. 请求(request) - 返回每个HTTP请求的一个Bean实例
  4. 会话(session) - 返回每个HTTP会话的一个bean实例
  5. 全局会话(globalSession)- 返回全局HTTP会话的一个bean实例
在大多数情况下,可能只处理了 Spring 的核心作用域 - 单例和原型,默认作用域是单例。
注:意味着只有在一个基于web的Spring ApplicationContext情形下有效!
单例VS原型
这里有一个例子来说明,bean的作用域单例和原型之间的不同:
package com.yiibai.customer.services;

public class CustomerService
{
String message; public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
}
}
1.单例例子
如果 bean 配置文件中没有指定 bean 的范围,默认为单例。
<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="customerService" class="com.yiibai.customer.services.CustomerService" /> </beans>

执行结果:

package com.yiibai.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.yiibai.customer.services.CustomerService; public class App
{
public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"}); CustomerService custA = (CustomerService)context.getBean("customerService");
custA.setMessage("Message by custA");
System.out.println("Message : " + custA.getMessage()); //retrieve it again
CustomerService custB = (CustomerService)context.getBean("customerService");
System.out.println("Message : " + custB.getMessage());
}
}

输出结果

Message : Message by custA
Message : Message by custA

由于 bean 的 “CustomerService' 是单例作用域,第二个通过提取”custB“将显示消息由 ”custA' 设置,即使它是由一个新的 getBean()方法来提取。在单例中,每个Spring IoC容器只有一个实例,无论多少次调用 getBean()方法获取它,它总是返回同一个实例。

2.原型例子
如果想有一个新的 “CustomerService”bean 实例,每次调用它的时候,需要使用原型(prototype)来代替。
<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="customerService" class="com.yiibai.customer.services.CustomerService" scope="prototype"/>
</beans>

运行-执行

Message : Message by custA
Message : null
在原型作用域,必须为每个 getBean()方法中调用返回一个新的实例。
3. Bean作用域注释
还可以使用注释来定义 bean 的作用域。
package com.yiibai.customer.services;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; @Service
@Scope("prototype")
public class CustomerService
{
String message; public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
}
}
启用自动组件扫描
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.yiibai.customer" /> </beans>

request作用域:

表示该针对每一次HTTP请求都会产生一个新的bean,仅适用于WebApplicationContext环境。

<bean id="bean的id" class="bean的包名.类名" scope="request"/>

Spring1以上提供

session作用域:

表示该针对每一次HTTP请求都会产生一个新的bean,仅适用于WebApplicationContext环境。

<bean id="bean的id" class="bean的包名.类名" scope="session"/>

Spring1以上提供

globalSession作用域:

它仅仅在基于portlet的web应用中才有意义。Portlet规范定义了全局Session的概念,它被所有构成某个portlet
web应用的各种不同的portlet所共享。在global session作用域中定义的bean被限定于全局portlet
Session的生命周期范围内。如果你在web中使用global
session作用域来标识bean,那么,web会自动当成session类型来使用。

<bean id="bean的id" class="bean的包名.类名" scope="globalSession"/>

Spring1以上提供

Web环境作用域的特殊配置:

使用request作用域、request作用域、globalSession作用域还需要进行额外的配置

在低版本的Web容器中(Servlet2.3以前),需要使用过滤器进行配置

在高版本的Web容器中,可以使用监听器进行配置

    <web-app>
...
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>

混合使用作用域的问题:

a Bean是Request作用域,它要被一个singleton作用域的b Bean使用(被注入)。还要使用<aop:sclped-proxy/>配置。

<beans xmlns="..."

...

xmlns:aop="....."   要引入aop命名空间

>

<bean id="a" class="A" scope="request">

<aop:sclped-proxy/>

</bean>

<bean id="b" class="B" scope="singleton">

<property name="a" ref="a"/>

</bean>

最新文章

  1. MS SQL执行大脚本文件时,提示“内存不足”的解决办法()
  2. .NET Core Web 应用部署到 Docker 中运行
  3. Word Excel 操作总结
  4. 分享Kali Linux 2016.2第48周镜像文件
  5. 如何用 Parse 和 Swift 搭建一个像 Instagram 那样的应用?(2)
  6. Apache加载PHP.ini顺序
  7. 北大ACM(POJ1005-I Think I Need a Houseboat)
  8. https://lua-toolbox.com/
  9. [Twisted] 部署Twisted
  10. Jquery JSOPN在WebApi中的问题
  11. gridview列显示,截取其中前面的几个字显示出来,当鼠标放上去的时候显示全部——使用LinkButton的方法
  12. Djanto static静态文件配置
  13. MongoDB学习总结(三) —— 常用聚合函数
  14. Ubuntu 16.04 安装wine QQ
  15. H5分享功能
  16. 申请Let&#39;s Encrypt永久免费SSL证书
  17. 吴裕雄 python 机器学习——岭回归
  18. vs 调试 iis中的网站
  19. 关于 extern &quot;C&quot;的说明
  20. Bootstrap学习目录

热门文章

  1. 20165302实验二java面向对象程序设计
  2. UESTC - 1987 童心未泯的帆宝和乐爷 (第k短路 A*算法+SPFA算法 模板)
  3. 推荐一个Oracle数据库学习网站
  4. VUE 跳转另一页面返回之前页面刷新,但数据依然存在
  5. Redis 持久化深入--机制、可靠性及比较
  6. Java实例 Part5:面向对象入门
  7. 初识hadoop之分布式文件系统(HDFS)
  8. BLDC无刷直流电机的原理及驱动基础
  9. PCB布线设计(1)
  10. Ambari搭建hadoop错误记录