在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. Java项目:化妆品商城系统(java+Springboot+ssm+mysql+jsp+maven)
  2. java jquery的定义方法_jQuery--基本语法
  3. Ubuntu代理上网软件cntlm
  4. 分支定义之Trunk vs Master
  5. (JAVA)复制文件test.txt,并且排序。文件重新命名为test1.txt
  6. 玩转iOS开发:iOS 10 新特性《Thread Sanitizer》
  7. Python 处理 CSV/EXCEL 表格文件
  8. 对PostgreSQL中bgwriter的 MyProc 的理解
  9. java.io.FileNotFoundException异常,一是“拒绝访问”,二是“系统找不到指定路径”
  10. 8.0魔兽服务器维护时间,魔兽世界8.0大米开放时间一览_wow8.0大秘境开启时间介绍_3DM网游...
  11. pdf电脑地址转网络地址.txt
  12. php $path_info,PHP $_SERVER['PATH_INFO'] 无法获取到内容怎么办?
  13. matlab脑电打码,matlab 脑电信号特征提取程序
  14. 3dmax 计算机中丢失,3dmax材质丢失怎么快速找回-解决3dmax材质不见了的方法 - 河东软件园...
  15. 使用WampServer搭建自己的网站
  16. TimescaleDB Continuous Aggregates介绍
  17. python乒乓球比赛规则介绍_乒乓球比赛规则简单介绍
  18. iPhone8 和 iPhoneX 买哪个?听我的
  19. golang 获取当天0点时间_golang 获取当天是周几(两种方法)
  20. Java 虚拟机:Java 内存区域及对象,java 反射面试

热门文章

  1. 当代最值得收藏的画家作品_欣赏当代知名画家谭日群——国画人物画书法作品欣赏...
  2. typescript 接口 java_[Java教程]【TypeScript】TypeScript 学习 2——接口
  3. mysql基于.frm和.ibd进行mysql数据恢复
  4. lucene和elasticsearch的前世今生、elasticsearch的核心概念、elasticsearch核心概念 vs. 数据库核心概念(来自学习资料)
  5. 启动Spark Shell,在Spark Shell中编写WordCount程序,在IDEA中编写WordCount的Maven程序,spark-submit使用spark的jar来做单词统计
  6. SQL Server 2017 2019 Linux(Centos、Unbuntu16.04、Unbuntu18.04、Unbuntu20.04)安装过程详解
  7. 怎么查看计算机一共多少文档,不打开Word文档如何查看文章有几页 -电脑资料
  8. 二分平均值聚类 java_二分K-均值聚类算法
  9. html块状元素高度,CSS:如何计算块元素的高度?
  10. ie统计报表html,IE中动态添加表格