警告:Spring ApplicationContext - Resource leak: 'context' is never closed的处理
来源:互联网 发布:手写图片识别软件 编辑:程序博客网 时间:2024/04/30 12:38
在一次代码调试中,发现了一条Warning提示:
Spring ApplicationContext - Resource leak: ‘context’ is never closed
看下面的代码:
<!-- 代码片段 -->ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:userLibrary.xml");service = ctx.getBean(UserLibrary.class);<!-- 代码片段 -->
上面的代码会产生一条Warning信息:
Resource leak: 'ctx' is never closed
这个警告是怎么产生的呢?
ctx对象没有类似close或者destroy方法。这个警告又试图告诉开发者什么信息呢?
处理这个警告的一种方法:
public class MainApp { /* * 这种处理方法并不是很恰当。因为放大了context的限定范围。 */ private static ApplicationContext context; public static void main(String[] args) { context = new ClassPathXmlApplicationContext("Beans.xml"); HelloWorld objA = (HelloWorld) context.getBean("helloWorld"); objA.setMessage("I'm object A"); objA.getMessage(); HelloWorld objB = (HelloWorld) context.getBean("helloWorld"); objB.getMessage(); }}
ClassPathXMLApplicationContext super class implements ConfigurableApplicationContext which contains the close() method. We can typecast the context into ConfigurableApplicationContext to call the close() method, it frees the resources. Simply also we can do like ((ClassPathXmlApplicationContext)ctx).close();
另外一种处理方法:
import org.springframework.context.ConfigurableApplicationContext;((ConfigurableApplicationContext)ctx).close();
0 0
- 警告:Spring ApplicationContext - Resource leak: 'context' is never closed的处理
- 【Spring】在Java使用Spring时的Resource leak: 'applicationContext' is never closed警告
- Spring实例化-Resource leak: 'context' is never closed
- Resource leak: 'context' is never closed
- 【eclipse_bug_001】ApplicationContext Resource leak: 'ctx' is never closed
- Java:Resource leak: 'a' is never closed怎么处理?
- ApplicationContext is never closed
- java出现Resource leak: 'input' is never closed 解决方案
- eclipse 之中出现了 Resource leak: 'in' is never closed
- java出现Resource leak: 'XXX' is never closed 解决方案
- Java遇到Resource leak: 'input' is never closed怎么解决
- java出现Resource leak: 'input' is never closed 问题
- eclipse中编写Java程序出现Resource leak: 'input' is never closed解决方法
- Spring:BeanFactory、ApplicationContext、Resource
- Error creating context 'spring.root': InputStream is null from Resource
- spring+hibernate:在applicationCOntext.XML中配置C3P0参数说明Attempted to use a closed or broken resource pool
- 去除performSelector的leak警告
- hibernate处理数据的时候:Session is closed!
- jekyll博客搭建
- Java中Runtime运行时环境机制总结
- hadoop部分异常处理
- 【解题报告】Codeforces Good Bye 2016
- Lattice系列FPGA入门相关1(Lattice系列FPGA简介)
- 警告:Spring ApplicationContext - Resource leak: 'context' is never closed的处理
- 这一年呐。。。
- spring-boot restful接口学习(1)
- Exjts 4.0 关于树与border布局显示的问题,急求结局
- 【C++解题报告】求组合数(定义函数)
- js post 清除REFERER 来路,全网首发
- Centos7 下禁用Mysql密码的强度加强validate_password插件
- 80x86微处理器第三章总结
- 【Java工具类】Runtime 例如,JAVA虚拟机在运行时,最大内存是多少