Spring中的destroy-method="close"- -2007-10-07 16:31究竟Spring在何时调用destroy-method="close" 这个方法close()呢?终于借助JavaEye找到了答案,原来如果Spring不在Web Container或是EJB Container中的时候,这个方法还是需要我们自己来调用的,具体就是调用BeanFactory的destroySingletons()方法,文档上的“自动调用”这几个字真是害我不浅呀,原来自动也是通过Web Container或是EJB Container才可以自动,具体做法就是要实现ServletContextListener这个接口,Spring中已经有具体的实现了:public class ContextLoaderListener implements ServletContextListener {
private ContextLoader contextLoader;
/**
* Initialize the root web application context.
*/
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
/**
* Create the ContextLoader to use. Can be overridden in subclasses.
* @return the new ContextLoader
*/
protected ContextLoader createContextLoader() {
return new ContextLoader();
}
/**
* Return the ContextLoader used by this listener.
*/
public ContextLoader getContextLoader() {
return contextLoader;
}
/**
* Close the root web application context.
*/
public void contextDestroyed(ServletContextEvent event) {
this.contextLoader.closeWebApplicationContext(event.getServletContext());
}
}
当tomcat关闭的时候会自动调用contextDestroyed(ServletContextEvent event)这个方法。在看一下contextLoader的closeWebApplicationContext方法: public void closeWebApplicationContext(ServletContext servletContext) throws ApplicationContextException {
servletContext.log("Closing root WebApplicationContext");
Object wac = servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (wac instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) wac).close();
}
}
AbstractApplicationContext.Close这个方法是要你自己调用的,在程序要结束的时候保证调用这个close方法,在这里的话就是由Listener来保证tomcat退出的时候调用close方法。
AbstractApplicationContext.Close的代码 :public void close() {
logger.info("Closing application context [" + getDisplayName() + "]");
// Destroy all cached singletons in this context,
// invoking DisposableBean.destroy and/or "destroy-method".
getBeanFactory().destroySingletons();
// publish corresponding event
publishEvent(new ContextClosedEvent(this));
}
最终还是调用到了getBeanFactory().destroySingletons(); 看来,没有容器,我们还是需要自己来搞定这个方法的调用的 !
分享到:
相关推荐
资源释放:<bean destroy-method=""/>仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用关系的控制. (3)DI概念 Dependecy ...
资源释放:<bean destroy-method=""/>仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用关系的控制. (3)DI概念 Dependecy ...
资源释放:<bean destroy-method=""/>仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用关系的控制. (3)DI概念 Dependecy ...
destroy-method="close"> <property name="driverClassName"> <value>org.gjt.mm.mysql.Driver <property name="url"> <value>jdbc:mysql://localhost:3306/demo <property name="username"> ...
<bean id="derby-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>org.apache.derby.jdbc.EmbeddedDriver <property name="url"> ...
JSP Spring中Druid连接池配置 jdbc.properties url=jdbc:postgresql://***.... <bean id=dataSource class=com.alibaba.druid.pool.DruidDataSource init-method=init destroy-method=close> <!-- 基本属性 url、u
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性 url、user、password --> <property name="url" value="${jdbc.url}" /> ...
<bean id="oracle-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" lazy-init="true"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> ...
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionImp" init-method="init" destroy-method="close"> <property name="transactionTimeout" value="300"/> <bean id=...
-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" lazy-init="false"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" ...
<bean id="transactionManager" class="com.atomikos.icatch.jta.UserTransactionImp" init-method="init" destroy-method="close"> <property name="transactionTimeout" value="300" /> <bean id=...
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/> <property name="jdbcUrl" ...
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db1.driver}" /> <property name="url" value="${db1.url}" /> ...
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" init-method="init" destroy-method="close"> <!-- 数据源配置 --> <property name="driverClassName" value="com.mysql.jdbc.Driver"/>...
<bean id="primaryDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- 配置数据源属性 --> <bean id="secondaryDataSource" class="org.apache.commons.dbcp....
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- 配置数据库连接信息 --> <bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" ...
- 对于需要在容器关闭时执行清理工作的Bean,可以定义`destroy-method`属性来指定销毁方法,如`<bean destroy-method="myDestroy"/>`。 - `DisposableBean`接口提供了`destroy()`方法,可由实现类覆盖以进行资源...
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" ...
<bean id="primaryDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- 数据源属性配置 --> <bean id="secondaryDataSource" class="org.apache.commons.dbcp....
c3po连接池jar包,测试过好用。 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass">