Spring中的destroy-method方法


1. Bean標簽的destroy-method方法

配置數據源的時候,會有一個destroy-method方法

Java 代碼 
  1. <bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      
  2.     <property name="driverClassName" value="${jdbc.driver}"></property>   
  3.         <property name="url" value="${jdbc.url}"></property>   
  4.         <property name="username" value="${jdbc.username}"></property>   
  5.         <property name="password" value="${jdbc.password}"></property>   
  6.         <property name="maxActive" value="${maxActive}"></property>   
  7.         <property name="maxWait" value="${maxWait}"></property>   
  8.         <property name="maxIdle" value="30"></property>   
  9.         <property name="initialSize" value="2"></property>   
  10. </bean> 

這個destroy-method屬性是干什么用的。什么時候調用呢?
Spring中的doc上是這么說destroy-method方法的---

Java代碼 
  1. The name of the custom destroy method to invoke on bean factory shutdown.
  2. The method must have no arguments, but may throw any exception. Note:
  3. Only invoked on beans whose lifecycle is under the full control of the factory - which
  4.  is always the case for singletons, but not guaranteed for any other scope. 

其實,這是依賴在Servlet容器或者EJB容器中,它才會被自動給調用的。比如我們用Servlet容器,經常在web.xml文件中配置這樣的監聽器

Java代碼 
  1. <listener>   
  2.    <listener-class>   
  3.     org.springframework.web.context.ContextLoaderListener   
  4.    </listener-class>   
  5. </listener>  

我們按層次包,看一下ContextLoaderListener這個類。

Java代碼 
  1. public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
      
  2.   //這個是web容器初始化調用的方法。   
  3.   public void contextInitialized(ServletContextEvent event) {   
  4.         this.contextLoader = createContextLoader();   
  5.         if (this.contextLoader == null) {   
  6.             this.contextLoader = this;   
  7.         }   
  8.         this.contextLoader.initWebApplicationContext(event.getServletContext());   
  9.     }   
  10.      
  11.   //這個是web容器銷毀時調用的方法。   
  12.   public void contextDestroyed(ServletContextEvent event) {   
  13.         if (this.contextLoader != null) {   
  14.             this.contextLoader.closeWebApplicationContext(event.getServletContext());   
  15.         }   
  16.         ContextCleanupListener.cleanupAttributes(event.getServletContext());   
  17.     }  
 

ContextLoaderListener實現了javax.servlet.ServletContextListener,它是servlet容器的監聽器接口。
ServletContextListener接口中定義了兩個方法。

Java代碼 
  1. public void contextInitialized(ServletContextEvent event);   
  2. public void contextDestroyed(ServletContextEvent event);  

分別在容器初始化或者銷毀的時候調用。
那么當我們銷毀容器的時候,其實就是調用的contextDestroyed方法里面的內容。
這里面執行了ContextLoader的closeWebApplicationContext方法。

Java代碼 
  1. public void closeWebApplicationContext(ServletContext servletContext) {   
  2.         servletContext.log("Closing Spring root WebApplicationContext");   
  3.         try {   
  4.             if (this.context instanceof ConfigurableWebApplicationContext) {   
  5.                 ((ConfigurableWebApplicationContext) this.context).close();   
  6.             }   
  7.         }   
  8.         finally {   
  9.             ClassLoader ccl = Thread.currentThread().getContextClassLoader();   
  10.             if (ccl == ContextLoader.class.getClassLoader()) {   
  11.                 currentContext = null;   
  12.             }   
  13.             else if (ccl != null) {   
  14.                 currentContextPerThread.remove(ccl);   
  15.             }   
  16.             servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);   
  17.             if (this.parentContextRef != null) {   
  18.                 this.parentContextRef.release();   
  19.             }   
  20.         }   
  21.     }  

這里面,將context轉型為ConfigurableWebApplicationContext,而
ConfigurableWebApplicationContext繼承自ConfigurableApplicationContext,在ConfigurableApplicationContext(ConfigurableApplicationContext實現了Lifecycle,正是前文提到的lifecycle)里有
close()方法的定義。在AbstractApplicationContext實現了close()方法。真正執行的是

Java代碼 
  1. protected void destroyBeans() {   
  2.     getBeanFactory().destroySingletons();   

方法(spring容器在啟動的時候,會創建org.apache.commons.dbcp.BasicDataSource的對象,放入singleton緩存中。那么在容器銷毀的時候,會清空緩存並調用BasicDataSourc中的close()方法。
)
BasicDataSourc類中的close()方法

Java代碼 
  1. public synchronized void close() throws SQLException {   
  2.         GenericObjectPool oldpool = connectionPool;   
  3.         connectionPool = null;   
  4.         dataSource = null;   
  5.         try {   
  6.             if (oldpool != null) {   
  7.                 oldpool.close();   
  8.             }   
  9.         } catch(SQLException e) {   
  10.             throw e;   
  11.         } catch(RuntimeException e) {   
  12.             throw e;   
  13.         } catch(Exception e) {   
  14.             throw new SQLNestedException("Cannot close connection pool", e);   
  15.         }   
  16.     }  

那么,如果spring不在Servlet或者EJB容器中,我們就需要手動的調用AbstractApplicationContext類中的close()方法,去實現相應關閉的功能。

 

轉:http://www.xuebuyuan.com/1628117.html 謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM