1. Spring中bean的多种作用域

在默认情况下,Spring应用上下文中所有的bean都是以单例(singleton)的形式创建的,即不管给定的一个bean被注入到其他bean多少次,每次所注入的都是同一个实例。

Spring定义了多种作用域,可以基于这些作用域创建bean:

  1. 单例(Singleton):在整个应用中,只创建bean的一个实例。
  2. 原型(Prototype):每次注入或者通过Spring应用上下文获取的时候,都会创建一个新的bean实例。
  3. 会话(Session):在Web应用中,为每个会话创建一个bean实例。
  4. 请求(Request):在Web应用中,为每个请求创建一个bean实例。

单例是默认的作用域,如果要使用其它的作用域创建bean,需要使用@Scope注解,该注解可以和@Component@Service@Bean等注解一起使用。

2. 示例

为了更好的理解,我们通过具体的代码示例来理解下Singleton与ProtoType的区别。

2.1 新建Singleton类型的bean

package chapter03.scope;

import org.springframework.stereotype.Service;

@Service
public class DemoSingletonService {
}

因为Spring默认配置的Scope是Singleton,因此以上代码等价于以下代码:

package chapter03.scope;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; @Service
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class DemoSingletonService {
}

上面的代码也可以写成@Scope("singleton")

2.2 新建ProtoType类型的bean

如果是自动装配bean,语法为:

package chapter03.scope;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; @Service
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DemoPrototypeService {
}

上面的代码也可以写成@Scope("prototype")

如果是在Java配置类中声明bean,语法为:

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public DemoPrototypeService demoPrototypeService() {
return new DemoPrototypeService();
}

如果是在xml中声明bean,语法为:

<bean id="demoPrototypeService" class="chapter03.scope.DemoPrototypeService"
scope="prototype"/>

2.3 新建配置类

package chapter03.scope;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan("chapter03.scope")
public class ScopeConfig {
}

2.4 测试

新建一个Main类,在main()方法中,分别从Spring容器中获取2次Bean,然后判断Bean的实例是否相等:

package chapter03.scope;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScopeConfig.class); DemoSingletonService s1 = context.getBean(DemoSingletonService.class);
DemoSingletonService s2 = context.getBean(DemoSingletonService.class); DemoPrototypeService p1 = context.getBean(DemoPrototypeService.class);
DemoPrototypeService p2 = context.getBean(DemoPrototypeService.class); System.out.println("s1 与 s2 是否相等:" + s1.equals(s2));
System.out.println("p1 与 p2 是否相等:" + p1.equals(p2)); context.close();
}
}

运行结果如下:

从运行结果也可以看出,默认情况下即Singleton类型的Bean,不管调用多少次,只会创建一个实例。

3. 注意事项

Singleton类型的Bean,@Scope注解的值必须是:singleton。

ProtoType类型的Bean,@Scope注解的值必须是:prototype。

即使是大小写不一致也不行,如我们将DemoPrototypeService类的代码修改为:

package chapter03.scope;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; @Service
@Scope("PROTOTYPE")
public class DemoPrototypeService {
}

此时运行代码,就会报错:

4. 源码及参考

源码地址:https://github.com/zwwhnly/spring-action.git,欢迎下载。

汪云飞《Java EE开发的颠覆者:Spring Boot实战》

原创不易,如果觉得文章能学到东西的话,欢迎点个赞、评个论、关个注,这是我坚持写作的最大动力。

如果有兴趣,欢迎添加我的微信:zwwhnly,等你来聊技术、职场、工作等话题(PS:我是一名奋斗在上海的程序员)。

Spring入门(五):Spring中bean的作用域的更多相关文章

  1. spring入门(五) spring mvc+hibernate

    核心是让SessionFactory由Spring管理 1.引入依赖 <!-- https://mvnrepository.com/artifact/org.springframework/sp ...

  2. Spring入门2. IoC中装配Bean

    Spring入门2. IoC中装配Bean 20131125 前言: 上一节学习了Spring在JavaProject中的配置,通过配置文件利用BeanFactory和ApplicationConte ...

  3. 7 -- Spring的基本用法 -- 5... Spring容器中的Bean;容器中Bean的作用域;配置依赖;

    7.5 Spring容器中的Bean 7.5.1 Bean的基本定义和Bean别名 <beans.../>元素是Spring配置文件的根元素,该元素可以指定如下属性: default-la ...

  4. spring中bean的作用域属性singleton与prototype的区别

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

  5. java——spring中bean的作用域

    文章:理解Spring框架中Bean的作用域 博客地址:https://baijiahao.baidu.com/s?id=1610298792072480906&wfr=spider& ...

  6. 详解Spring中Bean的作用域与生命周期

    摘要:在利用Spring进行IOC配置时,关于bean的配置和使用一直都是比较重要的一部分,同时如何合理的使用和创建bean对象,也是小伙伴们在学习和使用Spring时需要注意的部分,所以这一篇文章我 ...

  7. Spring核心技术(五)——Spring中Bean的作用域

    前文概述了Spring的容器,Bean,以及依赖的一些信息,本文将描述一下Bean的作用域 Bean的作用域 当开发者定义Bean的时候,同时也会定义了该如何创建Bean实例.这些具体创建的过程是很重 ...

  8. Spring中bean的作用域与生命周期

    在 Spring 中,那些组成应用程序的主体及由 Spring IOC 容器所管理的对象,被称之为 bean.简单地讲,bean 就是由 IOC 容器初始化.装配及管理的对象,除此之外,bean 就与 ...

  9. Spring中Bean的作用域和生命周期

    作用域的种类 Spring 容器在初始化一个 Bean 的实例时,同时会指定该实例的作用域.Spring3 为 Bean 定义了五种作用域,具体如下. 1)singleton 单例模式,使用 sing ...

随机推荐

  1. jenkins在windows上自动化部署.Net(.Net Core)项目

    前言 什么是持续集成呢?Continuous integration(CI).持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员至少集成一次,也就意味着每天可能会发生多次集成. ...

  2. linux 操作系统级别监控 nmon命令

    nmon是IBM公司开发的Linux性能监控工具,可以实时展示系统性能情况,也可以将监控数据写入文件中,并使用nmon分析器做数据展示 实时监控 命令 ./nmon c 代表CPU m 代表Memor ...

  3. C++基础之string类

    string也是属于顺序容器,但是string类很重要且经常使用,因此在这里单独记录. string的操作总结 string(const char *s,int n);  //用c字符串s初始化,s应 ...

  4. Leetcode 96.不同的搜索二叉树

    给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 1 3 3 2 1 \ / ...

  5. Audio Bit Depth Super-Resolution with Neural Networks

    Audio Bit Depth Super-Resolution with Neural Networks 作者:Thomas Liu.Taylor Lundy.William Qi 摘要 Audio ...

  6. Flask中的路由、实例化参数和config配置文件

    Flask中的路由 endpoint 别名不能重复,对应的视图函数,默认是视图函数名.endpoint 才是路由的核心.视图函数与路由的对应关系.可以通过url_for 反向创建url # metho ...

  7. nodeJs环境添加代理

    目的:实现前后端分离,前端减少路径请求的所需的路由文件: 第一步:安装http代理中间件 npm install http-proxy-middleware --save 第二步: express文件 ...

  8. 利用CSS制作图形效果

    前言 关于如何使用CSS来制作图形,比如说圆形,半圆形,三角形等的相关教程还是挺多的,今天我主要想解释一下里面一些demo的实现原理,话不多说,开始吧   以下所有内容只使用一个HTML元素.任何类型 ...

  9. ELK 学习笔记之 Logstash安装

    Logstash安装: https://www.elastic.co/downloads/logstash 下载解压: tar –zxvf logstash-5.6.1.tar.gz 在/usr/lo ...

  10. MongoDB 学习笔记之 replica set搭建

    Replica set搭建: 修改mongodb.conf文件,指明replSet 登入客户端,指定副本集成员,进行初始化, 如果priority需要调整,使用reconfig()方法.Seconda ...