在停止
或取消部署/重新部署
基于Spring框架3.0.5
的Web应用程序时,Tomcat7的catalina中会记录以下错误.out
:
SEVERE: The web application [/nomination##1.0-qa] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@4f43af8f]) and a value of type [org.springframework.security.core.context.SecurityContextImpl] (value [org.springframework.security.core.context.SecurityContextImpl@ffffffff: Null authentication]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
我最初想到在那里实现 ServletContextListener
和 close()
上下文。然而,发现实现了 ServletContextListener
的 ContextLoaderListener
是像 web.xml
中这样设置的:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
来自 Javadocs:
**contextDestroyed**
public void contextDestroyed(ServletContextEvent event)
Close the root web application context.
Specified by:
contextDestroyed in interface ServletContextListener
所以,我的问题是为什么 ThreadLocal 没有被 ContextLoaderListener.contextDestroyed() 干净地释放?
我们遇到了 PermGen
错误,并在调查时发现了这一点。有几个地方有类似下面的代码:
ApplicationContext context = WebApplicationContextUtils
.getWebApplicationContext(se.getSession().getServletContext());
MyBeanClass x = context.getBean(
"myBean", MyBeanClass.class);
x.someMethod();
我想知道上面的代码片段是否阻止了干净的关闭? 任何指点将不胜感激。
最佳答案
hotdeploy 有很多问题需要您注意:
1-注销数据库驱动程序 see here .
2-在多任务应用程序中关闭任务:您可以在开发模式下延迟近 1 小时的重新启动。
3-杀死 spring 的上下文:上面已经做了,但注意不要导入 XML 形式的 XML 。
4-Kill JVM内存中存在的缓存对象:make小对象,你正在初始化吗 构造函数中的bean?将其更改为无构造函数以继续在方法范围内杀死它们! 类中有多少个方法调用bean?如果许多方法调用bean不希望java在从作用域出来后杀死对象,jvm将保留它以提高性能......,所以让你的类保持小!。
你的代码怎么样?您是否将变量声明为循环外?使用后你是=null列表还是对象?
5-您可以增加tomcat的启动时间和停止时间。
也可以将rebel或springboot项目视为助手。
关于spring-mvc - 为什么 Spring Context 没有优雅地关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16464420/