为什么Spring Context没有优雅地关闭?

kma*_*oor 10 memory-leaks spring-mvc permgen servlet-listeners servletcontextlistener

stopundeploy/redeploy一的Spring framework 3.0.5基于Web的应用程序中记录以下错误Tomcat7's 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.
Run Code Online (Sandbox Code Playgroud)

我最初想到实现ServletContextListenerclose()那里的背景.但是,发现ContextLoaderListener哪个工具ServletContextListener设置如下web.xml:

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)

来自Javadocs:

**contextDestroyed**

public void contextDestroyed(ServletContextEvent event)
Close the root web application context.
Specified by:
contextDestroyed in interface ServletContextListener
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是为什么ThreadLocal没有干净利落ContextLoaderListener.contextDestroyed()

我们遇到了PermGen错误,而调查发现了这一点.在以下几个地方有类似的代码:

ApplicationContext context = WebApplicationContextUtils
            .getWebApplicationContext(se.getSession().getServletContext());

MyBeanClass x = context.getBean(
            "myBean", MyBeanClass.class);
x.someMethod(); 
Run Code Online (Sandbox Code Playgroud)

我想知道上面的片段是否正在停止干净关闭?任何指针将不胜感激.

Ali*_*edy 0

hotdeploy 有很多你应该关心的问题:

1-注销数据库驱动程序请参见此处

2-在多任务应用程序中关闭任务:您可以在开发模式下延迟近 1 小时的重新启动。

3-杀死 spring 的上下文:你在上面做了,但注意不要导入 XML 形式的 XML 。

4-杀死JVM内存中存在的缓存对象:制作小对象,您是否在构造函数中初始化bean?将其更改为无构造函数以继续在方法范围内杀死它们!类中有多少个方法调用bean?如果许多方法调用bean不希望java在退出范围后杀死对象,jvm将保留它以提高性能......,所以让你的类保持小!

你的代码怎么样?您是否将变量声明为循环外?使用后你是=null列表还是对象?

5-您可以增加tomcat的启动时间和停止时间。

还可以将rebel 或springboot 项目视为helper 。