在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的作用域单例和原型之间的不同:
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/beanshttp://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 againCustomerService 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/beanshttp://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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:component-scan base-package="com.yiibai.customer" /></beans>
下载代码 – http://pan.baidu.com/s/1o7jxMrg
http://www.yiibai.com/spring/spring-bean-scopes-examples.html

Spring Bean作用域实例相关推荐

  1. spring bean作用域_Spring面试知识点,这是我见过最全面的 - 知识铺

    知识铺: 致力于打造轻知识点,持续更新每次的知识点较少,阅读不累.不占太多时间,不停地来唤醒记忆深处的知识点. Q1.什么是Spring框架? Spring是最流行的企业应用程序框架之一.Spring ...

  2. Spring Bean 作用域之间的区别?

    Spring 容器中的bean 可以分为5 个范围.所有范围的名称都是自说明的,但是为了避免混淆,还是让我们来解释一下: 1.singleton:这种bean 范围是默认的,这种范围确保不管接受到多少 ...

  3. spring:Bean作用域

    在配置文件中定义Bean时,用户不但可以配置Bean的属性值及相互之间的依赖关系,还可以定义Bean的作用域.作用域将对Bean的生命周期和创建方式产生影响. spring 4.0中所支持的作用域: ...

  4. Struts2 Action 与Spring bean 作用域

    struts2 的action 是没有scope的,但通过引用spring bean 可以达到有scope功能. <action name="xxxAction" class ...

  5. spring bean作用域_Srping中Bean的三种装配方式:大魏Java记10

    一.Bean的作用域 Spring在初始化一个Bean实例时,可以同时为其指定特定的作用域.作用域将会对Bean的生命周期和创建方式产生影响. Bean的作用域类型: Singleton作用域是Spr ...

  6. Spring Bean作用域与生命周期

    目录 Bean的作用域: Bean有六大行为模式 1.singleton:单例模式(默认) 2.prototype: 原型模式(多例模式) 3.request: 请求作用域(Spring MVC) 4 ...

  7. ​Spring IOC中 Bean 作用域

    ​Spring Bean 作用域 Spring 3 中为Bean定义了5种作用域,它们是:singleton(单例).prototype(原型).request.session 和 global se ...

  8. Spring bean相关

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

  9. Spring的作用域与生命周期

    文章目录 一.lombok的安装与使用 二.Spring作用域 二.Bean原理分析 执行流程 Bean的生命周期 一.lombok的安装与使用 lombok插件可以提供给我们一些注释,这些注释可以很 ...

最新文章

  1. c#拼图碎片形状_拼图游戏C#代码
  2. 12种提升视频质量的方法
  3. 小米4刷centos_给大家推荐两款小米的产品
  4. linux 扩展挂载盘大小_Linux 添加挂载硬盘(包含挂载大于2T以上硬盘)
  5. 那些和闰年相关的 Bug
  6. Oracle sqlserver mysql的自增变量设置
  7. vue 指令 v-once
  8. android 拉伸view,安卓ImageView拉伸展示
  9. PIP scrapydo时报错ERROR: Command errored out with exit status 1: python setup.py egg_info Check the log
  10. python求均值函数_python求列表平均值函数
  11. 什么快捷键切换仅计算机,什么是电脑屏幕切换快捷键
  12. 卡特兰数(Catalan UVa 991 10303 10007 1478)[11]
  13. 优化模型验证关键代码06:多行程旅行商问题(mTSP)
  14. cpu超线程优缺点_CPU有无超线程重要吗?i7 10700K与9700K对比测试
  15. 一名小程序员的2021年度 个人总结
  16. 视频播放插件AVPro1-插件介绍
  17. 怎样给电脑文件夹批量快速重新命名?
  18. linux中如何看文件换行符,linux下的换行符
  19. 下划线的作用(python)
  20. (附源码)springboot 房产中介系统 毕业设计 312341

热门文章

  1. HTTP 2.0与HTTP 1.0的区别 ?
  2. C语言 其他标准函数
  3. 下pg负载均衡_SAE 场景下,应用流量的负载均衡及路由策略配置实践
  4. java数据结构有哪些_java有哪些数据结构?
  5. 史上最简单的SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)
  6. 16、分布式文档系统--document的_source元数据以及定制返回结果解析(来自学习资料+自己整理)
  7. 自定义Flume拦截器,并将收集的日志存储到Kafka中(案例)
  8. sqlplus连接远程数据库
  9. JavaGUI中的JComboBox的处理
  10. 窗口分析函数_2_生成同值重复排名序号