Bean的作用域

简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean

通过Spring容器创建一个Bean的实例时,不仅可以完成Bean的实例化,还可以为Bean指定特定的作用域。

那什么是Bean的作用域?Bena的作用域有什么用呢?

官网:https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-factory-scopes

  1. 代理模式(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"/>
  2. 原型模式:每次从容器中get的时候,都会产生一个新的对象!

    <bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
  3. 其余的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的作用域的更多相关文章

  1. java 从spring容器中获取注入的bean对象

      java 从spring容器中获取注入的bean对象 CreateTime--2018年6月1日10点22分 Author:Marydon 1.使用场景 控制层调用业务层时,控制层需要拿到业务层在 ...

  2. 从spring容器中取出注入的bean

    从spring容器中取出注入的bean 工具类,代码如下: package com.hyzn.fw.util; import org.springframework.beans.BeansExcept ...

  3. Spring框架中的单例bean是线程安全的吗?

    不,Spring框架中的单例bean不是线程安全的.

  4. Spring 框架中的单例 bean 是线程安全的吗?

    不,Spring 框架中的单例 bean 不是线程安全的.

  5. 关于 Spring Boot 中创建对象的疑虑 → @Bean 与 @Component 同时作用同一个类,会怎么样?

    开心一刻 今天放学回家,气愤愤地找到我妈 我:妈,我们班同学都说我五官长得特别平 妈:你小时候爱趴着睡觉 我:你怎么不把我翻过来呢 妈:那你不是凌晨2点时候出生的吗 我:嗯,凌晨2点出生就爱趴着睡觉呗 ...

  6. Spring学习过程中遇到的No bean named 'beanId' is defined报错

    ApplicationContext applicationContext= new ClassPathXmlApplicationContext("bean.xml");Obje ...

  7. 如何动态在spring mvc中增加bean

    阅读对象 搭框架人员,或者其他感兴趣的开发人员 背景 一般来说在业务代码中,加上 @Component, @Service,@Repository, @Controller等注解就可以实现将bean注 ...

  8. 获取Spring容器中的Bean

    摘要 SpringMVC框架开发中可能会在Filter或Servlet中用到spring容器中注册的java bean 对象,获得容器中的java bean对象有如下方法 Spring中的Applic ...

  9. Spring中三种配置Bean的方式

    Spring中三种配置Bean的方式分别是: 基于XML的配置方式 基于注解的配置方式 基于Java类的配置方式 一.基于XML的配置 这个很简单,所以如何使用就略掉. 二.基于注解的配置 Sprin ...

  10. 【String注解驱动开发】如何按照条件向Spring容器中注册bean?这次我懂了!!

    写在前面 当bean是单实例,并且没有设置懒加载时,Spring容器启动时,就会实例化bean,并将bean注册到IOC容器中,以后每次从IOC容器中获取bean时,直接返回IOC容器中的bean,不 ...

随机推荐

  1. Java中构造代码块的使用

    例子1 public class Client { { System.out.println("执行构造代码块1"); } { System.out.println("执 ...

  2. Oracle数据导入Mysql中

    一.Navicat Premium中的数据迁移工具 为了生产库释放部分资源,需要将API模块迁移到mysql中,及需要导数据. 尝试了oracle to mysql工具,迁移时报错不说,这么大的数据量 ...

  3. 【QT】多个槽函数绑定同一个信号的触发顺序

    目录 一.Qt 3.0(包含3.0) - Qt 4.5(包含4.5)版本之前 二.Qt 4.6(包含4.6)版本之后 一.Qt 3.0(包含3.0) - Qt 4.5(包含4.5)版本之前 「多个槽函 ...

  4. Scala中的IO操作及ArrayBuffer线程安全问题

    通过Scala对文件进行读写操作在实际业务中应用也比较多,这里介绍几种常用的方式,直接上代码: 1. 从文件中读取内容 object Main { def loadData(): Array[Stri ...

  5. [日常摸鱼]HDU1348Wall-凸包

    我学习进度慢得连我自己都怕- 题意:大概给$n$个点搞出它的凸包,然后还要在凸包外弄一层厚为$l$的东西,求这个东西的周长 我个滞涨居然把pi开成了int-搞了一个晚上才看见 凸包直接求,因为是凸多边 ...

  6. 关于BAPI_ACC_DOCUMENT_POST解读

    BAPI_ACC_DOCUMENT_POST是SAP ERP提供生成会计凭证的标准BAPI,这个BAPI可以用到多种场景生成会计凭证,实际项目中一般情况下更多的是生成应收和应付的会计凭证,分别对应客户 ...

  7. javascript笔记day01

    JavaScript基础语法 HTML :标记语言 JavaScript :编程语言 序言 JavaScript发展历史(JS) 1. 1994年,网景公司(Netscape)发布了Navigator ...

  8. Python -- 修改、添加和删除元素

    大多数列表将是动态的,这意味着列表创建后,将随着程序的运行增删元素. 修改列表元素 修改列表元素的语法与访问列表元素的语法类似.要修改列表元素,可指定表名和要修改的元素指引,再指定该元素的新值. #代 ...

  9. day021|python之面向对象进阶1

    面向对象进阶 目录 面向对象进阶 1 继承 1.1 继承入门 1.1.1 继承基础 1.1.2 类的基本使用 1.2 多继承 1.2.1 多继承的基本使用 1.2.2 多继承以后的重复性 1.3 类的 ...

  10. C盘满了删除C盘文件

    还有很多文件在C:\Users\lock\AppData 比如C:\Users\lock\AppData\Local\Temp  临时文件 C:\Users\lock\AppData\Roaming\ ...