Spring applicationContext爆出警告“Resource leak: 'applicationContext' is never closed”
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
此处applicationContext会有警告出现,根据警告的英文意思翻译成中文就是“applicationContext没有关闭”,和Scanner,DB,IO类似,用完即关,所以我们要把applicationContext关闭。而applicationContext没有直接提供关闭的方法,在它的子类“ConfigurableApplicationContext”中,则提供了close()方法,于是可以用((ConfigurableApplicationContext)applicationContext).close();
警告则不再出现。
当然,比较装逼的写法也有:applicationContext.getClass().getMethod("close").invoke(applicationContext);
或者既然都要获取bean,要去new“ClassPathXmlApplicationContext”了,那何不把“ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);”整句修改了,把ApplicationContext直接替换成ClassPathXmlApplicationContext,更为直接明了呢,于是可以修改成如下:
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
applicationContext.close(); // 注:好像Spring高版本不可以如此使用
可以了解Spring的生命周期以便更清晰的了解
Spring applicationContext爆出警告“Resource leak: 'applicationContext' is never closed”的更多相关文章
- Eclipse用java.util.Scanner时出现Resource leak: 'in' is never closed
Resource leak: 'in' is never closed : 直译为资源泄漏,‘in’一直没被关闭. 由于声明了数据输入扫描仪(Scanner in),从而获得了配置内存,但是结束时却没 ...
- Java关于Resource leak: 's' is never closed的问题
Resource leak: 's' is never closed的问题 问题:在编写Java时出现了Resource leak: 's' is never closed的问题,也就是对象s下面的波 ...
- Resource leak: 'context' is never closed
from: http://stackoverflow.com/questions/14184059/spring-applicationcontext-resource-leak-context-is ...
- Sturts2整合Spring报错:org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml];
十一月 17, 2019 1:11:44 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin警告: [SetPropertiesRul ...
- 【JAVA错误笔记】 - 【Could not open ServletContext resource [/WEB-INF/applicationContext.xml]解决方法】
错误描述: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] 原因分析: 问题主要由于加载spring的 ...
- Could not open ServletContext resource [/WEB-INF/applicationContext.xml]解决方法
抛错: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document ...
- 【Spring源码分析系列】ApplicationContext 相关接口架构分析
[原创文章,转载请注明出处][本文地址]http://www.cnblogs.com/zffenger/p/5813470.html 在使用Spring的时候,我们经常需要先得到一个Applicati ...
- spring IOC源码分析(ApplicationContext)
在上一篇文章中,我们以BeanFactory这条主线进行IOC的源码解析的,这里,将以ApplicationContext这条线进行分析.先看使用方法: @Test public void testA ...
- 【Spring】3、BeanFactory 和 ApplicationContext的区别
转自:http://blog.csdn.net/intlgj/article/details/5660587 在spring中,两个最基本最重要的包是 org.springframework.bean ...
随机推荐
- SpringCloud Sleuth入门介绍
案例代码:https://github.com/q279583842q/springcloud-e-book 一.Sleuth介绍 为什么要使用微服务跟踪?它解决了什么问题? 1.微服务的现状? ...
- Git 所有常用命令
写的很细致,存: https://blog.csdn.net/Mr_Lewis/article/details/85547057
- You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'wher id = 41' at line 1
where 没有打完整
- LNet代码分析
源码版本:lustre-release 2.15.55 介绍 LNet是Lustre的网络模块,代码目录分为了lnet和lnd lnet提供了统一的接口 lnd封装了底层驱动,有socklnd(TCP ...
- Oracle Awr报告_awr报告解读_基础简要信息
导出 关于awr报告的导出,上一篇博客已经进行过讲述了.博客链接地址:https://www.cnblogs.com/liyasong/p/oracle_report1.html 这里就不再赘述. ...
- ZigBee按键查询实践
按键查询 即硬件上电后,程序开始运行,当检测到按键按下,触发按键语句,执行按键触发的事件: 玩单片机需要清楚两件东西,第一个是单片机的电路图,另一个则是单片机的寄存器: CC2530是51的升级版,我 ...
- JavaSE核心知识
一:Java简介 1. Java语言的介绍:Java是一门面向对象编程语言,具有功能强大和简单易用两个特征. 2. Java语言的特点:简单性.面向对象.分布式.健壮性.安全性.平台独立与可移植性.多 ...
- C语言学习书籍推荐《C语言入门经典(第5版)》下载
霍尔顿 (Ivor Horton) (作者), 杨浩 (译者) 下载地址:点我 C语言是每一位程序员都应该掌握的基础语言.C语言是微软.NET编程中使用的C#语言的基础:C语言是iPhone.iPad ...
- Java多线程(六):wait(),notify()和notifyAll()
wait(),notify()和notifyAll()介绍 1.wait() 使当前线程等待,直到另一个线程调用notify(),notifyAll()或者中断,当前线程调用wait()之前必须持有锁 ...
- APP系统架构设计初探
一,图片体验的优化. 在手机上显示图片,速度是一个非常重要的体验点,试想,如果您打开一个网站,发现里面的图片一直显示失败或者是x,稍微做得好一点的,可能是一个不消失的loading或者是菊花等等,但不 ...