Spring中的destroy-method方法
1. Bean标签的destroy-method方法
配置数据源的时候,会有一个destroy-method方法
- <bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="${jdbc.driver}"></property>
- <property name="url" value="${jdbc.url}"></property>
- <property name="username" value="${jdbc.username}"></property>
- <property name="password" value="${jdbc.password}"></property>
- <property name="maxActive" value="${maxActive}"></property>
- <property name="maxWait" value="${maxWait}"></property>
- <property name="maxIdle" value="30"></property>
- <property name="initialSize" value="2"></property>
- </bean>
这个destroy-method属性是干什么用的。什么时候调用呢?
Spring中的doc上是这么说destroy-method方法的---
- The name of the custom destroy method to invoke on bean factory shutdown.
- The method must have no arguments, but may throw any exception. Note:
- Only invoked on beans whose lifecycle is under the full control of the factory - which
- is always the case for singletons, but not guaranteed for any other scope.
其实,这是依赖在Servlet容器或者EJB容器中,它才会被自动给调用的。比如我们用Servlet容器,经常在web.xml文件中配置这样的监听器
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
我们按层次包,看一下ContextLoaderListener这个类。
- public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
- //这个是web容器初始化调用的方法。
- public void contextInitialized(ServletContextEvent event) {
- this.contextLoader = createContextLoader();
- if (this.contextLoader == null) {
- this.contextLoader = this;
- }
- this.contextLoader.initWebApplicationContext(event.getServletContext());
- }
- //这个是web容器销毁时调用的方法。
- public void contextDestroyed(ServletContextEvent event) {
- if (this.contextLoader != null) {
- this.contextLoader.closeWebApplicationContext(event.getServletContext());
- }
- ContextCleanupListener.cleanupAttributes(event.getServletContext());
- }
ContextLoaderListener实现了javax.servlet.ServletContextListener,它是servlet容器的监听器接口。
ServletContextListener接口中定义了两个方法。
- public void contextInitialized(ServletContextEvent event);
- public void contextDestroyed(ServletContextEvent event);
分别在容器初始化或者销毁的时候调用。
那么当我们销毁容器的时候,其实就是调用的contextDestroyed方法里面的内容。
这里面执行了ContextLoader的closeWebApplicationContext方法。
- public void closeWebApplicationContext(ServletContext servletContext) {
- servletContext.log("Closing Spring root WebApplicationContext");
- try {
- if (this.context instanceof ConfigurableWebApplicationContext) {
- ((ConfigurableWebApplicationContext) this.context).close();
- }
- }
- finally {
- ClassLoader ccl = Thread.currentThread().getContextClassLoader();
- if (ccl == ContextLoader.class.getClassLoader()) {
- currentContext = null;
- }
- else if (ccl != null) {
- currentContextPerThread.remove(ccl);
- }
- servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
- if (this.parentContextRef != null) {
- this.parentContextRef.release();
- }
- }
- }
这里面,将context转型为ConfigurableWebApplicationContext,而
ConfigurableWebApplicationContext继承自ConfigurableApplicationContext,在ConfigurableApplicationContext(ConfigurableApplicationContext实现了Lifecycle,正是前文提到的lifecycle)里有
close()方法的定义。在AbstractApplicationContext实现了close()方法。真正执行的是
- protected void destroyBeans() {
- getBeanFactory().destroySingletons();
- }
方法(spring容器在启动的时候,会创建org.apache.commons.dbcp.BasicDataSource的对象,放入singleton缓存中。那么在容器销毁的时候,会清空缓存并调用BasicDataSourc中的close()方法。
)
BasicDataSourc类中的close()方法
- public synchronized void close() throws SQLException {
- GenericObjectPool oldpool = connectionPool;
- connectionPool = null;
- dataSource = null;
- try {
- if (oldpool != null) {
- oldpool.close();
- }
- } catch(SQLException e) {
- throw e;
- } catch(RuntimeException e) {
- throw e;
- } catch(Exception e) {
- throw new SQLNestedException("Cannot close connection pool", e);
- }
- }
那么,如果spring不在Servlet或者EJB容器中,我们就需要手动的调用AbstractApplicationContext类中的close()方法,去实现相应关闭的功能。
转:http://www.xuebuyuan.com/1628117.html 谢!
Spring中的destroy-method方法的更多相关文章
- Spring中RestTemplate的使用方法
一.REST 在互联网中,我们会通过请求url来对网络上的资源做增删改查等动作,这里的请求包含两部分:动词,主要包括增.删.改.查:名词,就是网络中的各种资源.传统的非REST风格的请求方式是把动词和 ...
- 面试官:spring中定义bean的方法有哪些?我一口气说出了12种,把面试官整懵了。
前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...
- 【Spring Framework】12种spring中定义bean的方法
前言 在庞大的java体系中,spring有着举足轻重的地位,它给每位开发者带来了极大的便利和惊喜.我们都知道spring是创建和管理bean的工厂,它提供了多种定义bean的方式,能够满足我们日常工 ...
- SSM-Spring-12:Spring中NameMatchMethodPointcutAdvisor名称匹配方法切入点顾问
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- advice 是通知advisor 是顾问 顾问(Advisor) 通知Advice是Spring提供的一种切 ...
- spring中的多线程aop方法拦截
日常开发中,常用spring的aop机制来拦截方法,记点日志.执行结果.方法执行时间啥的,很是方便,比如下面这样:(以spring-boot项目为例) 一.先定义一个Aspect import org ...
- Spring中@Async注解实现“方法”的异步调用
原文:http://www.cnblogs.com/zhengbin/p/6104502.html 简单介绍: Spring为任务调度与异步方法执行提供了注解支持.通过在方法上设置@Async注解,可 ...
- spring中得到servletContext对象方法
1.spring得到servletContext,这个和session没有什么关系,上下文可以说是一个session容器,一个上下文可以有多个会话session 在web.xml中有以下配置后.加入s ...
- spring 中StoredProcedure的使用方法
StoredProcedure是一个抽象类,必须写一个子类来继承它,这个类是用来简化JDBCTemplate运行存储过程操作的. 首先我们写一个实现类: package com.huaye.frame ...
- Spring中使用Ehcache的方法和注意事项
如何调用方法数据增加缓存 @Cacheable(value="MY_CACHE", key="'cache_business_' + #business_id" ...
- Spring中获取Session的方法汇总
Spring: web.xml <listener> <listener-class>org.springframework.web.context.request.Reque ...
随机推荐
- js便签笔记(7)——style、currentStyle、getComputedStyle区别介绍【转载】
转者语: 今天看jQuery源码CSS部分,里面用到了currentStyle和getComputedStyle来获取外部样式. 因为elem.style.width只能获取elem的style属性里 ...
- Disruptor多个消费者不重复处理生产者发送过来的消息
1.定义事件事件(Event)就是通过 Disruptor 进行交换的数据类型. package com.ljq.disruptor; import java.io.Serializable; /** ...
- 百度&高德地图小区景点边界轮廓实现
经常的我们在使用地图功能时,会发现在选择一个小区或者一个热门景点的时候,地图上面会给出其边界轮廓,能够方便我们知道其范围大小,有时候在我们使用地图组件的时候,也会面临着类似的需求.比如在地图上面标识出 ...
- 比特币算法——SHA256算法介绍
SHA256是安全散列算法SHA(Secure Hash Algorithm)系列算法之一,其摘要长度为256bits,即32个字节,故称SHA256.SHA系列算法是美国国家安全局 (NSA) 设计 ...
- Core Animation之基础介绍
了解了图层,现在学习核心动画. Core Animation是直接作用在CALayer上的,并非UIView. 一.使用步骤 1.使用它需要先添加QuartzCore.framework框架和引入主头 ...
- c# 检查目录,当指定目录不存在时建立目录
/// <remark> /// 检查目录,当指定目录不存在时建立目录 /// </remark> public static void CheckFolder(string ...
- [javaSE] 看知乎学习反射
简单的来说,反射机制指的是程序在运行时能够获取自身的信息.在java中,只要给定类的名字,那么就可以通过反射机制来获得类的所有信息. 知乎:学习java应该如何理解反射? 余晖: 反射提供了一种运 ...
- Navicat相关应用及注意事项
一.MySQL数据类型 1.数值型 SMALLINT: 2个字节 INT: 4个字节 // age int(10) INTEGER:INT的同义词 BIGINT : 8个字节 FLOAT : ...
- 从实例角度分析java的public、protected、private和default访问权限
一.public 同一个package 1.本类内部 public class A { public int f=1; public void m1() {} public void m2() { f ...
- UI-12组结对编程作业总结
UI-12组结对编程作业总结 源码Github地址 https://github.com/tilmto/TILMTO/tree/master/Arithmetic 作业摘要 本次结对编程作业分为以下两 ...