码迷,mamicode.com
首页 > 其他好文 > 详细

Resource leak: 'context' is never closed

时间:2016-11-18 07:06:50      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:sed   this   eclipse   void   color   stack   support   ram   question   

from: http://stackoverflow.com/questions/14184059/spring-applicationcontext-resource-leak-context-is-never-closed

Since the app context is a ResourceLoader (i.e. I/O operations) it consumes resources that need to be freed at some point. It is also an extension of AbstractApplicationContext which implements Closable. Thus, it‘s got a close() method and can be used in a try-with-resources statement.

try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/userLibrary.xml")) {
  service = context.getBean(UserLibrary.class);
}

Whether you actually need to create this context is a different question (you linked to it), I‘m not gonna comment on that.

It‘s true that the context is closed implicitly when the application is stopped but that‘s not good enough. Eclipse is right, you need to take measures to close it manually for other cases in order to avoid classloader leaks.

Resource leak: 'context' is never closed

标签:sed   this   eclipse   void   color   stack   support   ram   question   

原文地址:http://www.cnblogs.com/liqipeng/p/6076168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!