日期:2014-05-19  浏览次数:20657 次

关于spring的destroy-method不能设置的问题!
为什么我在配置applicationContext.xml的时候,写数据源
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" destroy-method="close">
加上destory-methd=close就报错!  
之前的项目都是好的。为什么啊!很奇怪的。
在线等!谢谢!

------解决方案--------------------
调用destroy-method="close" 这个方法close(),就是调用BeanFactory的destroySingletons()方法,具体做法就是要实现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(); 看来,没有容器,我们还是需要自己来搞定这个方法的调用的 !