使用@Bean注解,在不配置destroyMethod时,其默认值为:

String destroyMethod() default AbstractBeanDefinition.INFER_METHOD;
public static final String INFER_METHOD = "(inferred)";

也就是在不配置destroyMethod时,spring会使用推断的销毁方法,这种推断的方法要求满足:

1. public的

2. 无参数

3. 方法名为close或shutdown

如果当一个bean正好有上面的方法,那么就会在销毁时调用。比如redis.clients.jedis.BinaryJedis 及子类就满足要求,有一个shutdown方法。但是他的shutdown方法是向redis-server发送shutdown命令,并不是销毁连接。因此在这个Bean销毁时,其实是不希望调用该shutdown方法的。

如果想防止调用推断的销毁方法,需要给destroyMethod赋值为"":

@Bean(destroyMethod = "")

接下来让我们看看推断的销毁方法是如何生效的。

首先,在创建bean时(见org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean方法),会调用:

        // Register bean as disposable.
try {
registerDisposableBeanIfNecessary(beanName, bean, mbd);
}

该方法会检查销毁的方法(requiresDestruction里),并且注册DisposableBeanAdapter,DisposableBeanAdapter会最终调用bean的destroyMethod。

protected void registerDisposableBeanIfNecessary(String beanName, Object bean, RootBeanDefinition mbd) {
AccessControlContext acc = (System.getSecurityManager() != null ? getAccessControlContext() : null);
if (!mbd.isPrototype() && requiresDestruction(bean, mbd)) {
if (mbd.isSingleton()) {
// Register a DisposableBean implementation that performs all destruction
// work for the given bean: DestructionAwareBeanPostProcessors,
// DisposableBean interface, custom destroy method.
registerDisposableBean(beanName,
new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc));
}
else {
// A bean with a custom scope...
Scope scope = this.scopes.get(mbd.getScope());
if (scope == null) {
throw new IllegalStateException("No Scope registered for scope name '" + mbd.getScope() + "'");
}
scope.registerDestructionCallback(beanName,
new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc));
}
}
}

其他逻辑就显而易见了,源码就不贴了

bean的shutdown的更多相关文章

  1. Spring实战3:装配bean的进阶知识

    主要内容: Environments and profiles Conditional bean declaration 处理自动装配的歧义 bean的作用域 The Spring Expressio ...

  2. Spring高级装配bean

    目录 spring profile 条件化的bean声明 自动装配与歧义性 bean的作用域 Spring表达式语言 一.环境与profile 配置profile  bean 在软件开发的时候,有一个 ...

  3. bean 的各个属性

    http://www.springframework.org/schema/beans/spring-beans.xsd org.springframework.beans.factory.confi ...

  4. 【译】Spring 4 基于TaskScheduler实现定时任务(注解)

    前言 译文链接:http://websystique.com/spring/spring-job-scheduling-with-scheduled-enablescheduling-annotati ...

  5. Spring4.0编程式定时任务配置

    看过很多定时调度的配置,大多使用XML配置,觉得比较麻烦,也比较老套.这里介绍一种基于spring4.0注解编程式配置定时任务,简单清晰,使用方便.. 至于引入spring相关jar这里不多说,直接切 ...

  6. SpringBoot Schedule 配置

    1. 定时任务实现方式 定时任务实现方式: Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行 ...

  7. 160922、配置:spring通过profile或@profile配置不同的环境(测试、开发、生产)

    一.配置环境 applicationContext.xml中添加下边的内容(develop:开发环境,production:生产环境,test:测试环境) 注意:profile的定义一定要在文档的最下 ...

  8. Spring3.0.6定时任务

    项目使用的Spring版本比较旧是3.0.6版本,由于需要进行定时任务,就决定使用Spring自带的scheduled task. 在网上找了很多文章,也查看了Spring3.0.6的官方文档,按照网 ...

  9. spring-boot + Ehcache without XML

    http://stackoverflow.com/questions/21944202/using-ehcache-in-spring-4-without-xml 1.Ehcache配置类 @Conf ...

随机推荐

  1. NopCommerce源代码分析之用户验证和权限管理

    目录 1.  介绍 2.  UML 2.1  实体类UML图 2.2  业务相关UML图 3.  核心代码分析 3.1  实体类源代码 3.2  业务相关源代码 3.3  相关控制器源代码 3.4  ...

  2. Winform中使用FastReport的PictureObject时通过代码设置图片源并使Image图片旋转90度

    场景 FastReport安装包下载.安装.去除使用限制以及工具箱中添加控件: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  3. 在vue里使用codemirror的两种用法

    这是我自己做的一个左边点击对应的标题,右边显示相应代码的一个功能.代码显示这里用的是vue-codemirror插件. 第一种用法: 1.安装:npm install vue-codemirror - ...

  4. this.$route和this.$router区别

    this.$route 和 this.$router 这两个对象有什么区别: this.$route 是当前路由跳转对象,包含当前路由的name.path.query.params等属性 this.$ ...

  5. 21个React开发神器

    摘要: React开发神器. 原文:22 Miraculous Tools for React Developers in 2019 译者:前端小智 下列工具中的重要性与排序无关. 1.Webpack ...

  6. The listener supports no services oracle注册监听

    问题登场: [oracle@my-e450 ~]$ lsnrctl status …… The listener supports no servicesThe command completed s ...

  7. Linux驱动开发常用调试工具---之内存读写工具devmem和devkmem【转】

    转自:https://blog.csdn.net/gatieme/article/details/50964903 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原 ...

  8. 3-14 Pandas绘图

      1.魔法指令:%matplotlib inline :数据画图 In [1]: %matplotlib inline import pandas as pd In [2]: import nump ...

  9. 10、shell编程+流程控制+分支嵌套

    SHELL 编程     shell 是一个命令解释器,侦听用户指令.启动这些指令.将结果返回给用户(交互式的shell)     shell 也是一种简单的程序设计语言.利用它可以编写一些系统脚本. ...

  10. nginx源码安装与使用

    [root@localhost ~]# yum -y install pcre-devel zlib-devel openssl openssl-devel gcc* [root@localhost ...