1. Bean標籤的destroy-method方法html
配置數據源的時候,會有一個destroy-method方法java
- <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方法的--- web
- 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文件中配置這樣的監聽器 spring
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
咱們按層次包,看一下ContextLoaderListener這個類。 apache
- public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
-
- public void contextInitialized(ServletContextEvent event) {
- this.contextLoader = createContextLoader();
- if (this.contextLoader == null) {
- this.contextLoader = this;
- }
- this.contextLoader.initWebApplicationContext(event.getServletContext());
- }
-
-
- 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方法。 this
- 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()方法。真正執行的是 url
- protected void destroyBeans() {
- getBeanFactory().destroySingletons();
- }
方法(spring容器在啓動的時候,會建立org.apache.commons.dbcp.BasicDataSource的對象,放入singleton緩存中。那麼在容器銷燬的時候,會清空緩存並調用BasicDataSourc中的close()方法。
)
BasicDataSourc類中的close()方法 spa
- 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()方法,去實現相應關閉的功能。xml
轉:http://www.xuebuyuan.com/1628117.html 謝!