浏览 3613 次
|
该帖已经被评为新手帖
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2004-12-28
自己写了个简单的测试程序,加了个close()方法,以测试是在何时调用这个方法的,结果发现无论如何触发,这个方法是都无法被调用的,除非加上这句话:
factory.destroySingletons(); 大概看了源代码,主要是把Singleton对象从HashSet中remove(),并且调用对应的close()方法,然后我又查了Without EJB,在使用连接池的时候使用了destroy-method="close",并且发现了这句话: Note that the myDataSource definition specifies a destroy-method: The bean factory will automatically invoke the close() method of BasicDataSource on shutdown. 就是说这个方法是在on shutdown的时候容器自动调用的,那么shutdown应该如何理解呢?我现在的疑问是,这个方法是怎么触发的呢?因为我自己在测试的时候无论怎样都触发不了这个方法,除非调用factory.destroySingletons();? 想了好久,不甚理解呀,郁闷! 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2004-12-28
AbstractApplicationContext.Close
|
|
| 返回顶楼 | |
|
最后更新时间:2004-12-28
singleton="false"试试
|
|
| 返回顶楼 | |
|
最后更新时间:2004-12-28
gigix 写道 singleton="false"试试
factory.destroySingletons();这里面的代码只是remove() Singletons,remove后会触发destroy-method="close",如果连Singleton都不是,那就更不可能会触发destroy-method="close"了,而且我试过了,确实不行 |
|
| 返回顶楼 | |
|
最后更新时间:2004-12-28
jjx 写道 AbstractApplicationContext.Close
如果可以通过这个方法来触发的话,那么BeanFactory又没有这个方法,那如何触发呢? 而且,你如何保证容器会调用AbstractApplicationContext.Close这个方法呢? 我对文档上说的自动调用甚是不解,如何个自动法呢? |
|
| 返回顶楼 | |
|
最后更新时间:2004-12-28
举个例子,ContextLoaderListener的源代码,
我们知道,如果要在tomcat里面使用spring的话需要这个Listener(或者ContextLoaderServlet) [code:1]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()); } } [/code:1] 当tomcat关闭的时候会自动调用contextDestroyed(ServletContextEvent event)这个方法。在看一下contextLoader的closeWebApplicationContext方法: [code:1] 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(); } } [/code:1] 就应该明白文档里面是什么意思了,AbstractApplicationContext.Close这个方法是要你自己调用的,在程序要结束的时候保证调用这个close方法,在这里的话就是由Listener来保证tomcat退出的时候调用close方法。 AbstractApplicationContext.Close的代码 [code:1] 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)); }[/code:1] 其实就是调用context里面的beanFactory的destroySingletons()方法了,这个没什么好说的。我的意思就是,容器本身不知道什么时候要shutdown了,这个消息是要靠外部(程序员)来提供的。 PS:偶说的容器是指ApplicationContext,不知道你说的容器是不是tomcat之类的web container,MS说法有点不统一,呵呵。 |
|
| 返回顶楼 | |
|
最后更新时间:2004-12-28
明白了,谢谢啊! 嘎嘎
|
|
| 返回顶楼 | |
|
最后更新时间:2007-04-06
我怎么没有看到factory.destroySingletons()方法呢?
|
|
| 返回顶楼 | |
|
最后更新时间:2007-04-06
spring-src in eclipse
destroy-method right click reference project |
|
| 返回顶楼 | |










