在Spring中,bean作用域用于确定哪种类型的 bean 实例应该从Spring容器中返回给调用者。bean支持的5种范围域:
  1. 单例 - 每个Spring IoC 容器返回一个bean实例
  2. 原型- 当每次请求时返回一个新的bean实例
  3. 请求 - 返回每个HTTP请求的一个Bean实例
  4. 会话 - 返回每个HTTP会话的一个bean实例
  5. 全局会话- 返回全局HTTP会话的一个bean实例
在大多数情况下,可能只处理了 Spring 的核心作用域 - 单例和原型,默认作用域是单例。
注:意味着只有在一个基于web的Spring ApplicationContext情形下有效!
单例VS原型
这里有一个例子来说明,bean的作用域单例和原型之间的不同:
  1. package com.yiibai.customer.services;
  2.  
  3. public class CustomerService
  4. {
  5. String message;
  6.  
  7. public String getMessage() {
  8. return message;
  9. }
  10.  
  11. public void setMessage(String message) {
  12. this.message = message;
  13. }
  14. }
1.单例例子
如果 bean 配置文件中没有指定 bean 的范围,默认为单例。
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5.  
  6. <bean id="customerService"
  7. class="com.yiibai.customer.services.CustomerService" />
  8.  
  9. </beans>

执行结果:

  1. package com.yiibai.common;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. import com.yiibai.customer.services.CustomerService;
  7.  
  8. public class App
  9. {
  10. public static void main( String[] args )
  11. {
  12. ApplicationContext context =
  13. new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
  14.  
  15. CustomerService custA = (CustomerService)context.getBean("customerService");
  16. custA.setMessage("Message by custA");
  17. System.out.println("Message : " + custA.getMessage());
  18.  
  19. //retrieve it again
  20. CustomerService custB = (CustomerService)context.getBean("customerService");
  21. System.out.println("Message : " + custB.getMessage());
  22. }
  23. }

输出结果

  1. Message : Message by custA
  2. Message : Message by custA 

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

2.原型例子
如果想有一个新的 “CustomerService”bean 实例,每次调用它的时候,需要使用原型(prototype)来代替。
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5.  
  6. <bean id="customerService" class="com.yiibai.customer.services.CustomerService"
  7. scope="prototype"/>
  8.  
  9. </beans>

运行-执行

  1. Message : Message by custA
  2. Message : null
在原型作用域,必须为每个 getBean()方法中调用返回一个新的实例。
3. Bean作用域注释
还可以使用注释来定义 bean 的作用域。
  1. package com.yiibai.customer.services;
  2.  
  3. import org.springframework.context.annotation.Scope;
  4. import org.springframework.stereotype.Service;
  5.  
  6. @Service
  7. @Scope("prototype")
  8. public class CustomerService
  9. {
  10. String message;
  11.  
  12. public String getMessage() {
  13. return message;
  14. }
  15.  
  16. public void setMessage(String message) {
  17. this.message = message;
  18. }
  19. }
启用自动组件扫描
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  8.  
  9. <context:component-scan base-package="com.yiibai.customer" />
  10.  
  11. </beans>
 
http://www.yiibai.com/spring/spring-bean-scopes-examples.html

Spring Bean作用域实例的更多相关文章

  1. Spring 中 ApplicationContext 和 BeanFactory 的区别,以及 Spring bean 作用域

    //从ApplicationContext 中取 bean ApplicationContext ac = new ClassPathXmlApplicationContext ( "com ...

  2. 【Spring】IoC容器 - Spring Bean作用域Scope(含SpringCloud中的RefreshScope )

    前言 上一章学习了[依赖来源],本章主要讨论SpringBean的作用域,我们这里讨论的Bean的作用域,很大程度都是默认只讨论依赖来源为[Spring BeanDefinition]的作用域,因为在 ...

  3. Spring bean作用域

    全当知识要点记录了,大家随意踩踩. spring的作用域有以下几种singleton作用域prototype作用域request作用域session作用域global-session作用域 1. si ...

  4. [spring] -- bean作用域跟生命周期篇

    作用域 singleton : 唯一 bean 实例,Spring 中的 bean 默认都是单例的. prototype : 每次请求都会创建一个新的 bean 实例. request : 每一次HT ...

  5. java Spring bean作用域

    1. Singleton作用域 当一个bean的作用域为singleton, 那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则 ...

  6. Spring Bean 作用域

    Bean 的作用域 当在 Spring 中定义一个 bean 时,你必须声明该 bean 的作用域的选项.例如,为了强制 Spring 在每次需要时都产生一个新的 bean 实例,你应该声明 bean ...

  7. 第31章 Spring bean 作用域

    每日一句 I must say a word about fear. It is life's only true opponent. Only fear can defeat life. 这里必须说 ...

  8. Spring学习手札(四)谈谈Spring Bean的生命周期及作用域

    在Spring中,那些组成应用程序的主体以及由Spring IoC容器所管理的对象,被称之为Bean.Bean与应用程序中其他对象(比如自己创建类)的区别就是,Bean是由IoC容器创建于销毁的.在S ...

  9. Spring bean相关

    Spring中指定Bean的作用于的方式 以下四种为例: 单例(默认,可以不用特殊表明) @Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON) ...

随机推荐

  1. Java Eclipse 配置

    1.清除多余记录 最近用eclipse打包jar的时候,需要指定一个main函数.需要先运行一下main函数,eclipse的Runnable JAR File Specification 下的Lau ...

  2. [转载]Windows服务编写原理及探讨(1)

    有那么一类应用程序,是能够为各种用户(包括本地用户和远程用户)所用的,拥有用户授权级进行管理的能力,并且不论用户是否物理的与正在运行该应用程序的计算机相连都能正常执行,这就是所谓的服务了. (一)服务 ...

  3. 在Ubuntu上使用pip安装错误 read timed out 处理方法

    在终端输入 pip --default-timeout=1000 install -U pip 也就是修改超时时间.

  4. Tutorial 1: Serialization

    转载自:http://www.django-rest-framework.org/tutorial/1-serialization/#tutorial-1-serialization Tutorial ...

  5. Debian系网络配置 /etc/network/interfaces

    说Debian系的网卡配置跟Redhat系很不一样,Redhat是放在/etc/sysconfig/network-scripts目录下面的一大堆文件里面,要修改?你一个一个文件来过吧.Debian系 ...

  6. IDEA添加其他项目为库文件的方法

    IDEA添加其他项目为库文件的方法 2014年1月23日星期四 20:46 问题:如果我通过引用项目修改了被引用库项目的源代码,他没法自动编译,所以不会立即反应到引用项目. 在Eclipse中,很简单 ...

  7. log优化

    isLoggable(Level level) 包含计算的日志记录用isLoggable判断下. debug  info warn   error   ,一般记录error,  但是其他里面的计算还是 ...

  8. 【ios开发之疑难杂症】xcode运行出现SpringBoard 无法启动应用程序(错误:7)

    问题:xcode运行出现SpringBoard 无法启动应用程序(错误:7) 解决方案: 重启模拟器

  9. 字节对齐&&sizeof

    转:http://blog.chinaunix.net/uid-722885-id-124878.html 1. sizeof应用在结构上的情况 请看下面的结构: struct MyStruct { ...

  10. SGU 217. Two Cylinders

    题意:给空间内两根圆柱,求轴线垂直相交时公共部分的体积. 暴力积分即可. ID: Date'n'Time: Name: Task: .Ext: Status: Time: Memory: 158937 ...