在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. Vue实现购物小球抛物线

    .shop{ position: fixed; top: 300px; left: 40px; } .ball{ position: fixed; left: 32px; bottom: 22px; ...

  2. 【vue】饿了么项目-页面骨架开发

    1.页面骨架开发 1.1组件拆分 手机浏览器是把页面放在一个虚拟的“窗口”(viewport)中,通常这个虚拟的“窗口”(viewport)比屏幕宽,这样就不用把每个网页挤到很小的窗口中(这样会破坏没 ...

  3. linq的左连接右连接内连接用法

    1.左连接: var LeftJoin = from e in ListOfEmployees join d in ListOfDepartment on e.DeptID equals d.ID i ...

  4. [译] MVP模式的14条规则

    笔者在前文<MVP和MVC>中提到了两者的区别,以及MVP日趋流行的原因:即随着各种给力UI框架的发布,View的功能越来越强,已经足以完成一些简单的不需要与后台或其他view交互的eve ...

  5. linux挂在新硬盘

    login as: rootroot@192.168.109.128's password:Last login: Fri Mar 22 14:12:08 2019 from 192.168.109. ...

  6. SpringBoot 默认日志

    默认使用的这个类 org.apache.commons.logging.Log import org.apache.commons.logging.Log; import org.apache.com ...

  7. CC2540 低功耗串口, POWER_SAVING 模式 下 串口 0 的使用

    低功耗 模式 下 使用 串口 ,  因为 PM2 或者 PM3 状态下  32M晶振 是不工作 的,根据手册得知没有32M晶振, 串口是不能工作的,但是可以使用 外部中断,因此,我把  串口的接收引脚 ...

  8. DDL-数据类型

    一.数值型1.整型tinyint.smallint.mediumint.int/integer.bigint1         2        3          4            8 特 ...

  9. tomcat启动超时, Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds...

    本文转自于:http://www.cnblogs.com/yjhrem/articles/2955207.html

  10. QueryRunner cannot be resolved to a type:关于包不能正常导入的问题

    在操作一个功能模块的时候,出现一个问题: 我原则是按着项目指导一步一步走的,但却出现, QueryRunner cannot be resolved to a type,这个问题应该属于Xxx can ...