Resource leak: 'context' is never closed

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

时间: 2024-12-21 02:19:38

Resource leak: 'context' is never closed的相关文章

错误日志:Resource leak: 'xxx' is never closed(Scanner类的注意事项)

在使用Scanner类时,举个例子: 1 import java.util.Scanner; 2 public class data { 3 4 public static void main(String[] args){ 5 Scanner i=new Scanner(System.in); 6 float j=i.nextFloat(); 7 System.out.println(j); 8 } 9 } 出现警告 Resource leak: 'i' is never closed 错误原

使用字符串生成Bean,Resource 到Context

项目中需要验证一个XML文件生成的Bean是否正确,按照正常思路,自然是生成文件,再加载,try-catch一下 如: // 省略try-catch // 使用临时文件的位置 File tempFile = new File(FileUtils.getTempDirectoryPath()).concat("文件名"); // 写文件 FileUtils.write(tempFile,dalFile,"UTF-8"); // 获取Context Configurab

spring Stack Overflow

1. ApplicationContext 不关闭,资源泄露问题: Spring ApplicationContext - Resource leak: 'context' is never closed

Java用Scanner类获取用户输入

用Java编写程序时,有些数据需要用户输入,这个时候需要调用java提供的Scanner类,这个类在包java.util下,比如求一个矩形的面积,简单的看一下用法: 1 import java.util.Scanner; 2 public class Javashuru { 3 //Java获取用户输入,使用Scanner类,位于java.util包中 4 public static void main(String[] args){ 5 //创建Scanner对象 6 Scanner inpu

Java的输入和输出遇到的一点问题

1 //package com.yunying.test; 2 import java.io.Console; 3 import java.util.Scanner; 4 //学习io 5 public class LearnIO { 6 public static void main(String args[]) 7 { 8 @SuppressWarnings("resource") 9 Scanner in = new Scanner(System.in); 10 System.o

Python文摘:Python with Context Managers

原文地址:https://jeffknupp.com/blog/2016/03/07/python-with-context-managers/ Of all of the most commonly used Python constructs, context managers are neck-and-neck with decorators in a "Things I use but don't really understand how they work" contest

<Effective C++>读书摘要--Resource Management<一>

1.除了内存资源以外,Other common resources include file descriptors, mutex locks, fonts and brushes in graphical user interfaces (GUIs), database connections, and network sockets. Regardless of the resource, it's important that it be released when you're fini

内存泄漏(memory leak)和内存溢出

1. 什么是内存泄漏(memory leak)? 指由于疏忽或错误造成程序未能释放已经不再使用的内存的情况.内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,失去了对该段内存的控制,因而造成了内存的浪费. 2. 两种类型的内存泄漏: 堆内存泄漏(Heap leak).对内存指的是程序运行中根据需要分配通过malloc,realloc new等从堆中分配的一块内存,再是完成后必须通过调用对应的 free或者delete 删掉.如果程序的设计的错误导致这部分内存没有被释放,

SpringMVC 源代码深度解析<context:component-scan>(扫描和注册的注解Bean)

我们在SpringMVC开发项目中,有的用注解和XML配置Bean,这两种都各有自己的优势,数据源配置比较经常用XML配置,控制层依赖的service比较经常用注解等(在部署时比较不会改变的),我们经常比较常用的注解有@Component是通用标注,@Controller标注web控制器,@Service标注Servicec层的服务,@Respository标注DAO层的数据访问.SpringMVC启动时怎么被自动扫描然后解析并注册到Bean工厂中去(放到DefaultListableBeanF