使用@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. git报错 - remote: HTTP Basic: Access denied

    十年河东,十年河西,莫欺少年穷 学无止境,精益求精 git 拉取代码报: remote: HTTP Basic: Access denied,这是因为你的GIT密码修改后,需要重新认证授权,那么怎么操 ...

  2. jmeter对tomcat性能测试

    主要对tomcat的参数做一些记录(jmeter和tomcat在同一个计算机,可能引起测试误差) 我的计算机配置  4核8线程  8G内存 案例一 tomcat  JVM  1.8G堆内存,无数据库操 ...

  3. js 设计模式——代理模式

    代理模式 代理模式是为一个对象提供一个代用品或占位符,以便控制对它的访问. 生活中有很多的代理模式的场景.例如,明星有经纪人作为代理,老板有秘书作为代理等等,当有事情的时候,会找到经纪人或秘书,再由他 ...

  4. uni-app学习心得和填坑,关于uni-app 打包h5 页面的坑

    第一次使用博客园写博客 1.我写博客的原因,梳理知识,整理思路,好记性不如烂笔头做个记录吧!记录生活! 1.了解 大概在我使用hbuilder的时候,在官网浏览下载的hbuilder时候无意中发现了u ...

  5. CPU体系结构(组成部分)

    在准备网络工程师考试,里面有些知识点是比较常考的.自己写这篇博客呢,当作是笔记吧,自己看一看也分享给大家一起学习. 这部分的内容就是讲CPU里面的组成结构以及各部分的功能. CPU的构成:CPU主要由 ...

  6. 7.InfluxDB-InfluxQL基础语法教程--INTO子句

    本文翻译自官网,官网地址:(https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/) 通过INTO子句,可 ...

  7. 关于SQLite数据库 字段 DateTime 类型

    这两天刚接触SQLite 数据库 还没有太过于深入的了解 , 于是出现了一个问题 : 我在 C#中 ,使用SQLiteHelper 查询SQLite数据库数据时,报了这个错误: System.Form ...

  8. 学习CNN系列二:训练过程

    卷积神经网络在本质上是一种输入到输出的映射,它能够学习大量的输入与输出之间的映射关系,而不需要任何输入和输出之间精确的数学表达式,只要用已知的模式对卷积神经网络加以训练,网络就具有输入.输出之间映射的 ...

  9. pyhive client连接hive报错处理:Could not start SASL

    本来一切就绪,镜像里已安装如下主要的pip包. pyhive configparser pandas hdfs thrift sqlparse sasl thrift-sasl 但,使用pyhive ...

  10. v8 编译 时长3小时