【Spring】在Java使用Spring时的Resource leak: 'applicationContext' is never closed警告
来源:互联网 发布:绝爱后宫我知帝王心txt 编辑:程序博客网 时间:2024/05/01 21:26
在Java使用Spring的时候,在定义完Spring的核心文件,用准备使用ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");来启动Spring的时候,要是不处理,在eclipse必然会出现Resource leak: 'applicationContext' is never closed的警告。虽然程序是毫无疑问地能读完,但是这个警告看着注定不爽。
如下图所示:
这是因为,这个applicationContext使用完和Scanner等流一样,用完需要关闭,正如这个警告提示的字面意思的一样。
然而,你会发现,在applicationContext中并没有包含关闭方法,如下图所示:
这是因为Spring3.x将这个方法藏在ConfigurableApplicationContext这个类里面了。
引入org.springframework.context.ConfigurableApplicationContext这个类,同时最后加一句((ConfigurableApplicationContext)applicationContext).close();这个警告则可以完美去除了,也就是上面的程序变成了:
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.context.ConfigurableApplicationContext;public class Client {public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");// 中间,spring要做的一系列事情……((ConfigurableApplicationContext) applicationContext).close();}}
阅读全文
0 0
- 【Spring】在Java使用Spring时的Resource leak: 'applicationContext' is never closed警告
- 警告:Spring ApplicationContext - Resource leak: 'context' is never closed的处理
- Spring实例化-Resource leak: 'context' is never closed
- 【eclipse_bug_001】ApplicationContext Resource leak: 'ctx' is never closed
- java出现Resource leak: 'input' is never closed 解决方案
- Java:Resource leak: 'a' is never closed怎么处理?
- java出现Resource leak: 'XXX' is never closed 解决方案
- Java遇到Resource leak: 'input' is never closed怎么解决
- java出现Resource leak: 'input' is never closed 问题
- Resource leak: 'context' is never closed
- ApplicationContext is never closed
- eclipse 之中出现了 Resource leak: 'in' is never closed
- eclipse中编写Java程序出现Resource leak: 'input' is never closed解决方法
- spring+hibernate:在applicationCOntext.XML中配置C3P0参数说明Attempted to use a closed or broken resource pool
- Spring:BeanFactory、ApplicationContext、Resource
- java spring配置文件applicationcontext.xml的使用
- ApplicationContext 的使用时机 - [Spring]
- 如何在Java Web应用中获取Spring的ApplicationContext
- ThreadLocal技术
- spring 的父子容器解析
- 协同过滤算法实现
- MySql Order by 、Group by优化
- Openstack卷迁移状态图
- 【Spring】在Java使用Spring时的Resource leak: 'applicationContext' is never closed警告
- android res资源文件分包
- JVM GC 相关内容(待续)
- 安装 MySQL 后,需要调整的 10 个性能配置项
- Android监听发送短信
- pgsql 时间sql
- 【菜鸟】maven构建springmvc+mybatis+mysql【上】
- 命名实体识别
- 威力导演15旗舰版下载 |威力导演15中文破解版