在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>

Spring学习(七)-----Spring Bean的5种作用域的更多相关文章

  1. Spring学习之实例化bean的三种方式

    实例化bean的三种方式 构造器实例化bean Person.java public class Person { private String name; private Integer age; ...

  2. Spring学习(七)--Spring MVC的高级技术

    一.Spring MVC配置的替代方案 我们已经了解如何通过AbstractAnnotationConfigDispatcherServlet- Initializer快速搭建了Spring MVC环 ...

  3. spring学习七 spring和dynamic project进行整合

    spring和web项目进行整合,其实就是在项目启动时,就创建spring容器,然后在servlet中使用spring容器进行开. 注意:为了页面可以访问到servlet,因此servlet必须放进t ...

  4. spring中bean的五种作用域?Spring中的bean是线程安全的吗?

    spring中bean的五种作用域 当通过spring容器创建一个Bean实例时,不仅可以完成Bean实例的实例化,还可以为Bean指定特定的作用域.Spring支持如下5种作用域: singleto ...

  5. Spring学习(十一)-----Spring使用@Required注解依赖检查

    Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...

  6. Spring学习(六)-----Spring使用@Autowired注解自动装配

    Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...

  7. spring学习(03)之bean实例化的三种方式

    bean实体例化的三种方式 在spring中有三中实例化bean的方式: 一.使用构造器实例化:(通常使用的一个方法,重点) 二.使用静态工厂方法实例化: 三.使用实例化工厂方法实例化 第一种.使用构 ...

  8. Spring学习七----------Bean的配置之自动装配

    © 版权声明:本文为博主原创文章,转载请注明出处 Bean的自动装配(Autowiring) no:不启用自动装配,此时需要手动注入.参考:Spring学习三----------注入方式 defaul ...

  9. spring学习总结——装配Bean学习二(JavaConfig装配bean)

    通过Java代码装配bean 前言:上面梳理了通过注解来隐式的完成了组件的扫描和自动装配,下面来学习下如何通过显式的配置的装配bean: 使用场景:比如说,你想要将第三方库中的组件装配到你的应用中,在 ...

随机推荐

  1. window resize和scroll事件的基本优化

    转自 http://www.kankanews.com/ICkengine/archives/6564.shtml

  2. oracle11g之管理oracle数据库笔记(理论基础知识)

                                                                  第三章 管理oracle数据库 1.启动数据库步骤;(创建启动实例--> ...

  3. EF Core中外键关系的DeleteBehavior介绍(转自MSDN)

    Delete behaviors Delete behaviors are defined in the DeleteBehavior enumerator type and can be passe ...

  4. 前端基础-CSS是什么?

    阅读目录 一. 什么是CSS 二. 为何要用CSS 三. 如何使用CSS 一. 什么是CSS CSS全称Cascading Style Sheet层叠样式表,是专用用来为HTML标签添加样式的. 样式 ...

  5. SVN搭建(linux 7)

    第一步:通过yum命令安装svnserve,命令如下: >yum -y install subversion 此命令会全自动安装svn服务器相关服务和依赖,安装完成会自动停止命令运行 若需查看s ...

  6. 闲来无事做了一个项目,内有redis,EasyUI样式简单应用,七层分页查询,API跨域。

    <link href="~/jquery-easyui-1.5.3/themes/default/easyui.css" rel="stylesheet" ...

  7. C++练习 | 求解二叉树的高度

    int h(BTree *bt) { if(bt->lchild==NULL&&bt->rchild==NULL) ; if(bt->lchild!=NULL& ...

  8. Python 用hashlib。md5加密算法

    廖雪峰的官方网站学习MD5加密,网址:“http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a0 ...

  9. 双硬盘双系统win10+manjaro-kde搭建

    电脑sdd+hdd双硬盘,默认win10装在了sdd分区,uefi+gpt引导.现在想要在hdd中划分出一个分区安装manjaro,并在开机多重引导. 1. 制作安装盘 先去下载最新的镜像,最好在国内 ...

  10. jQuery.validate.js表单验证插件

    jQuery.validate.js表单验证插件的使用 效果: 代码: <!DOCTYPE html> <html lang="en"> <head& ...