【Spring】Spring中的Bean - 3、Bean的作用域
Bean的作用域
简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean
通过Spring容器创建一个Bean的实例时,不仅可以完成Bean的实例化,还可以为Bean指定特定的作用域。
那什么是Bean的作用域?Bena的作用域有什么用呢?

代理模式(Spring默认机制):get到的都是同一个对象!
<bean id="accountService" class="com.something.DefaultAccountService"/> <!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>
原型模式:每次从容器中get的时候,都会产生一个新的对象!
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
其余的request、session、application、这些个只能在web开发中使用。
作用域的种类
Spring 为Bean的实例定义了7种作用域,如下表所示:

注意:在上表7种作用域中,singleton和prototype是最常用的两种作用域。

这里只有六种哦 singleton、prototype、request、session、application和websocket
没了globalSession的
singleton单例作用域和prototype原型作用域。
singleton单例作用域
singleton是Spring容器默认的作用域,当Bean的作用域为singleton时,Spring容器就只会存在一个共享的Bean实例。
singleton作用域对于无会话状态的Bean(如Dao 组件、Service组件)来说,是最理想的选择。
在Spring配置文件中,Bean的作用域是通过元素的scope属性来指定的,该属性值可以设置为singleton、prototype、request、session、globalSession、application和websocket七个值,分别表示Bean的七种作用域。
在Spring中如何配置singleton作用域? 在Spring配置文件中,可以使用元素的scope属性,将Bean的作用域定义成singleton。
例如:
<bean id="scope" class="com.awen.scope.Scope" scope="singleton"/>
小案例来理解一下singleton作用域吧。
在项目chapter02中,创建一个com.awen.scope包,在包中创建Scope类,该类不需要写任何方法。
package com.awen.scope;
public class Scope {
}
在src/main/resources目录下,创建一个配置文件beans4.xml,将上述示例代码写入配置文件中。
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<bean id="scope" class="com.awen.scope.Scope" scope="singleton"/>
<!-- <bean id="scope" class="com.awen.scope.Scope" scope="prototype" />-->
</beans>
最后在com.awen.scope包中创建测试类ScopeTest,来测试singleton作用域,编辑后如文件所示。文件ScopeTest.java
package com.awen.scope;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class ScopeTest {
public static void main(String[] args) {
// 加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans4.xml");
// 输出获得实例
System.out.println(applicationContext.getBean("scope"));
System.out.println(applicationContext.getBean("scope"));
System.out.println(applicationContext.getBean("scope") == applicationContext.getBean("scope") );
Scope scope = (Scope)applicationContext.getBean("scope");
Scope scope2= (Scope) applicationContext.getBean("scope");
System.out.println(scope == scope2);
}
}
执行程序后,控制台的输出结果如下所示。
D:\Environments\jdk-11.0.2\bin\java.exe -javaagent:D:\Java\ideaIU-2019.2.win\lib\idea_rt.jar=1119:D:\Java\ideaIU-2019.2.win\bin -Dfile.encoding=UTF-8 -classpath D:\IdeaProjects\JavaEE-enterprise-application-development-tutorial\chapter02\target\classes;D:\Environments\apache-maven-3.6.2\maven-repo\javax\annotation\jsr250-api\1.0\jsr250-api-1.0.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-webmvc\5.2.3.RELEASE\spring-webmvc-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-aop\5.2.3.RELEASE\spring-aop-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-beans\5.2.3.RELEASE\spring-beans-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-context\5.2.3.RELEASE\spring-context-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-core\5.2.3.RELEASE\spring-core-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-jcl\5.2.3.RELEASE\spring-jcl-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-expression\5.2.3.RELEASE\spring-expression-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-web\5.2.3.RELEASE\spring-web-5.2.3.RELEASE.jar com.awen.scope.ScopeTest
com.awen.scope.Scope@74e52ef6
com.awen.scope.Scope@74e52ef6
true
true
Process finished with exit code 0
com.awen.scope.Scope@74e52ef6
com.awen.scope.Scope@74e52ef6
两次输出的结果相同,这说明Spring容器只创建了一个Scope类的实例。需要注意的是,如果不设置scope=“singleton”,其输出结果也是一个实例,因为Spring容器默认的作用域就是singleton。
prototype原型作用域
对需要保持会话状态的Bean(如Struts 2的Action类)应该使用prototype作用域。
在使用prototype作用域时,Spring容器会为每个对该Bean的请求都创建一个新的实例。
在Spring中如何配置prototype作用域?在Spring配置文件中,同样使用元素的scope属性,将Bean的作用域定义成prototype 。
例如
<bean id="scope" class="com.itheima.scope.Scope" scope=" prototype "/>
原型作用域
beans4.xml 改下
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
<!-- <bean id="scope" class="com.awen.scope.Scope" scope="singleton"/>-->
<bean id="scope" class="com.awen.scope.Scope" scope="prototype" />
</beans>
运行结果
D:\Environments\jdk-11.0.2\bin\java.exe -javaagent:D:\Java\ideaIU-2019.2.win\lib\idea_rt.jar=1185:D:\Java\ideaIU-2019.2.win\bin -Dfile.encoding=UTF-8 -classpath D:\IdeaProjects\JavaEE-enterprise-application-development-tutorial\chapter02\target\classes;D:\Environments\apache-maven-3.6.2\maven-repo\javax\annotation\jsr250-api\1.0\jsr250-api-1.0.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-webmvc\5.2.3.RELEASE\spring-webmvc-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-aop\5.2.3.RELEASE\spring-aop-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-beans\5.2.3.RELEASE\spring-beans-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-context\5.2.3.RELEASE\spring-context-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-core\5.2.3.RELEASE\spring-core-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-jcl\5.2.3.RELEASE\spring-jcl-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-expression\5.2.3.RELEASE\spring-expression-5.2.3.RELEASE.jar;D:\Environments\apache-maven-3.6.2\maven-repo\org\springframework\spring-web\5.2.3.RELEASE\spring-web-5.2.3.RELEASE.jar com.awen.scope.ScopeTest
com.awen.scope.Scope@6f03482
com.awen.scope.Scope@9d5509a
false
false
Process finished with exit code 0
运行结果可以看到,
com.awen.scope.ScopeTest
com.awen.scope.Scope@6f03482
com.awen.scope.Scope@9d5509a
两次输出的Bean实例并不相同,这说明在prototype作用域下,创建了两个不同的Scope实例。
【Spring】Spring中的Bean - 3、Bean的作用域的更多相关文章
- java 从spring容器中获取注入的bean对象
java 从spring容器中获取注入的bean对象 CreateTime--2018年6月1日10点22分 Author:Marydon 1.使用场景 控制层调用业务层时,控制层需要拿到业务层在 ...
- 从spring容器中取出注入的bean
从spring容器中取出注入的bean 工具类,代码如下: package com.hyzn.fw.util; import org.springframework.beans.BeansExcept ...
- Spring框架中的单例bean是线程安全的吗?
不,Spring框架中的单例bean不是线程安全的.
- Spring 框架中的单例 bean 是线程安全的吗?
不,Spring 框架中的单例 bean 不是线程安全的.
- 关于 Spring Boot 中创建对象的疑虑 → @Bean 与 @Component 同时作用同一个类,会怎么样?
开心一刻 今天放学回家,气愤愤地找到我妈 我:妈,我们班同学都说我五官长得特别平 妈:你小时候爱趴着睡觉 我:你怎么不把我翻过来呢 妈:那你不是凌晨2点时候出生的吗 我:嗯,凌晨2点出生就爱趴着睡觉呗 ...
- Spring学习过程中遇到的No bean named 'beanId' is defined报错
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("bean.xml");Obje ...
- 如何动态在spring mvc中增加bean
阅读对象 搭框架人员,或者其他感兴趣的开发人员 背景 一般来说在业务代码中,加上 @Component, @Service,@Repository, @Controller等注解就可以实现将bean注 ...
- 获取Spring容器中的Bean
摘要 SpringMVC框架开发中可能会在Filter或Servlet中用到spring容器中注册的java bean 对象,获得容器中的java bean对象有如下方法 Spring中的Applic ...
- Spring中三种配置Bean的方式
Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...
- 【String注解驱动开发】如何按照条件向Spring容器中注册bean?这次我懂了!!
写在前面 当bean是单实例,并且没有设置懒加载时,Spring容器启动时,就会实例化bean,并将bean注册到IOC容器中,以后每次从IOC容器中获取bean时,直接返回IOC容器中的bean,不 ...
随机推荐
- SpringBoot + SpringSecurity + Mybatis-Plus + JWT + Redis 实现分布式系统认证和授权(刷新Token和Token黑名单)
1. 前提 本文在基于SpringBoot整合SpringSecurity实现JWT的前提中添加刷新Token以及添加Token黑名单.在浏览之前,请查看博客: SpringBoot + Sp ...
- Centos7.8安装Redis
一.安装gcc依赖 由于 redis 是用 C 语言开发,安装之前必先确认是否安装 gcc 环境(gcc -v),如果没有安装,执行以下命令进行安装 [root@localhost local]# ...
- Docker(四):Docker安装Redis
查找Redis镜像 镜像仓库 https://hub.docker.com/ 下拉镜像 docker pull redis 查看镜像 docker images 创建Redis容器 运行Redis镜像 ...
- MySQL-实体映射关系和连接查询
一 数据库实体的映射关系 1. 一对一:学生表和学生档案表,一个学生只有一个对应的档案. 2. 一对多/多对一:一个球队友多个球员(一对多),多个球员属于一个球队(多对一) 3. 多对多:一个学生可 ...
- rbd锁引起kvm虚拟机无法启动的故障
前言 环境因为一些问题(网络,或者磁盘,或者其它各种异常),引起了集群的状态的一些变化,变化之后,集群的某些虚拟机正常某些虚拟机出现异常,异常现象就是无法启动 特别是win server2008 ,会 ...
- 点击劫持(Iframe clickJack)练习
实验内容: 寻找一个合适的网站放入到iframe标签中.实验中测试了包括知网首页及登录界面.淘宝首页及登录界面,百度首页,微信下载界面.发现淘宝登录界面无法放入,会直接跳转到淘宝真实的登录界面,其他的 ...
- 实验1 C语言开发环境使用和编程初体验
#include <stdio.h> #include <stdlib.h> int main() { printf ("202083290273\n2020 ,wh ...
- Sentinel滑动窗口算法
在前面搞清楚了Sentinel的使用后,大致理了一下Sentinel的责任链,搞清楚了这个,基本就已经梳理清楚sentinel-core模块的大部分内容,顺着这条链路可以继续梳理很多东西. 知其然.知 ...
- positive-unlabeled (PU) learning
PULearning的应用场景是,我们可以清晰地确定正样本,但是不能确定负样本,因为它有可能是正样本,只是我们还没有证明. 这时我们可以把这部分不确定的样本称为无标签样本U,加上正样本P来建立模型. ...
- 【探索之路】机器人篇(3)-给mwRobot建立模型
在创建一个mwRobot_description程序包那一节中,我们添加了依赖roscpp rospy std_msgs 和 urdf , 现在我们再添加一个xacro依赖. 如何添加依赖? 打开程 ...