我一直在尝试将我正在处理的 Tomcat 网络应用程序中的硬编码配置数据提取到 JNDI (web.xml) 中,以使该应用程序更具可配置性。我没有连续查找 InitialContext,而是使用单例缓存查找和 InitialContext。
当然,当我停止和启动应用程序时,我的单例的静态实例仍然存在——因此,缓存不会“刷新”。我的问题是双重的——我将这个单例隔离到单个 Web 应用程序——Tomcat 中是否有任何不可预见的影响[我读过单例和 tomcat 不混合]?当应用程序停止时清除单例实例的最简单方法是什么(我正在考虑编写一个单独的 ServerContextListener 来清除实例)。
(注意:如果我使用的术语不正确,我深表歉意——请更正——这是我第一次尝试使用 Tomcat 进行编程)
最佳答案
Instead of continuously doing lookups into the InitialContext, I'm caching the lookups and the InitialContext using a singleton
好的,所以你实现了一个 Service Locator对于缓存,这是 Web 层的常见做法。这是 an example .
Of course, when I stop and start the application, the static instance of my singleton remains alive
嗯……除非你在 Common class loader 中部署你的单例。 ($CATALINA_HOME/lib
) 它不应该。你没有这样做,对吧?
I'm isolating this singleton to a single web application -- are there any unforeseen effects in Tomcat
没有我知道的。
And what is the easiest way to clear the singleton instance when the application is stopped
在 webapp 类加载器中部署您的单例(即在 /WEB-INF/classes
或 /WEB-INF/lib
中)并按照建议取消部署应用程序。
关于java - Tomcat、单例和 ServerContextListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2967207/