【发布时间】:2019-01-08 17:04:35
【问题描述】:
我正在尝试关闭我的应用程序(这是一个 jar 并且手动运行)
Spring ApplicationContext - Resource leak: 'context' is never closed
因为我使用的是 spring 3.2.18
我的代码看起来像
private static ConfigurableApplicationContext parentContext ;
private static ConfigurableApplicationContext processContext ;
private final String processApplicationContextChild; // filename: childContext.xml
public void run() {
log.debug("Loading parent context");
parentContext = new ClassPathXmlApplicationContext("applicationContext.xml") ;
parentContext.registerShutdownHook();
try {
runProcess(parentContext);
} catch (Throwable e) {
log.error("Unexpected error: " + e.getMessage(), e);
} finally {
log.info("Closing Core Context ");
if (parentContext.isActive()){
parentContext.close();
}
log.info("Core Closed");
}
}
protected void runProcess(ConfigurableApplicationContext parentContext) {
log.info("Loading " + Arrays.asList(processApplicationContextChild));
processContext = new ClassPathXmlApplicationContext(processApplicationContextChild, parentContext);
processContext.registerShutdownHook();
Runnable processor = (Runnable) processContext.getBean("processorRunnable", Runnable.class);
try {
processor.run();
} finally {
log.info("Closing child Context ");
processContext.close();
log.info(" Child Closed");
}
}
“Core Closed”打印出来后。它只是挂起,什么也不做。我期待 JVM 退出。
在某些资源中,子资源或父资源未正确关闭。我如何查看该资源。或者这里可能是什么问题。 当我调试时,在每个 .close() 调用中,上下文仍然是一个 close=true 和 isActive=false 的实例。
我什至使它们成为非静态的,并尝试使用资源块。
【问题讨论】: