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”的更多相关文章

  1. Eclipse用java.util.Scanner时出现Resource leak: 'in' is never closed

    Resource leak: 'in' is never closed : 直译为资源泄漏,‘in’一直没被关闭. 由于声明了数据输入扫描仪(Scanner in),从而获得了配置内存,但是结束时却没 ...

  2. Java关于Resource leak: 's' is never closed的问题

    Resource leak: 's' is never closed的问题 问题:在编写Java时出现了Resource leak: 's' is never closed的问题,也就是对象s下面的波 ...

  3. Resource leak: 'context' is never closed

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

  4. 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 ...

  5. 【JAVA错误笔记】 - 【Could not open ServletContext resource [/WEB-INF/applicationContext.xml]解决方法】

    错误描述: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] 原因分析: 问题主要由于加载spring的 ...

  6. Could not open ServletContext resource [/WEB-INF/applicationContext.xml]解决方法

    抛错: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document ...

  7. 【Spring源码分析系列】ApplicationContext 相关接口架构分析

    [原创文章,转载请注明出处][本文地址]http://www.cnblogs.com/zffenger/p/5813470.html 在使用Spring的时候,我们经常需要先得到一个Applicati ...

  8. spring IOC源码分析(ApplicationContext)

    在上一篇文章中,我们以BeanFactory这条主线进行IOC的源码解析的,这里,将以ApplicationContext这条线进行分析.先看使用方法: @Test public void testA ...

  9. 【Spring】3、BeanFactory 和 ApplicationContext的区别

    转自:http://blog.csdn.net/intlgj/article/details/5660587 在spring中,两个最基本最重要的包是 org.springframework.bean ...

随机推荐

  1. QTcpSocket 对连接服务器中断的不同情况进行判定

    http://blog.csdn.net/goforwardtostep/article/details/52300335

  2. 在windows下的QT编程中的char*,wchar_t*与QString之间的转换(利用reinterpret_cast和_stprintf函数,fromWCharArray从字符数组里读取数据)

    http://blog.csdn.net/yangxiao_0203/article/details/7422660 转自http://hi.baidu.com/zj41342626/blog/ite ...

  3. pycharm窗口选项卡管理

    1.主题 我们已经注意到Pycharm的主编辑框是基于窗口选项卡机制显示的,Pycharm选项卡多种多样,这里我们将详细介绍这种选项卡机制.  2.激活的选项卡 每当我们打开一个Python文件时op ...

  4. java集合框架collection(3)Set、List和Map

    Set.List和Map是java collection中最常用的三种数据结构. Set是集合,不允许有重复的元素,List是动态数组实现的列表,有序可重复,Map是key-value的键值对,用于快 ...

  5. Verilog写一个对数计算模块Log2(x)

    网上一个能用的也没有,自己写一个把. 1.计算原理:  整数部分 网上找到了一个c语言的计算方法如下: int flog2(float x) { return ((unsigned&)x> ...

  6. Centos6 samba服务配置

    1.在阿里虚拟机中配置包源  在ecs的 /etc/yum.repos.d 创建个 alios.repo,内容如下 [alios.$releasever.base.$basearch] name=al ...

  7. vSphere克隆虚机重启网卡报错

    使用VMware vSphere克隆虚机,修改IP重启网卡报错: 解决报错: 修改 /etc/udev/rules.d/70-persistent-net.rules 文件,克隆后会多出eth2和et ...

  8. ZOJ 3963:Heap Partition(贪心+set+并查集)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3963 题意:给出一个n个数的序列,可以在其中找一个子序列建堆,并且堆中的父 ...

  9. java html中文汉字 反转义

    import org.apache.commons.lang.StringEscapeUtils; public class EscapeHtml { /** * @param args */ pub ...

  10. scrapy基础知识之 Logging:

    修改配置文件settings.py,任意位置添加 LOG_FILE = "XxSpider.log" LOG_LEVEL = "INFO" Log levels ...