Resource leak: 'context' is never closed

spring实例化时

 

[java] view plain copy
 
  1. public void test2() {  
  2.     ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
  3.     //配置文件中的id属性  
  4.     IHelloService service = (IHelloService) context.getBean("helloService");  
  5.     service.sayHello();  
  6.       
  7. }  


可以将前面用子类来写,并关闭

 

 

[java] view plain copy
 
    1. public void test2() {  
    2.     ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
    3.     //配置文件中的id属性  
    4.     IHelloService service = (IHelloService) context.getBean("helloService");  
    5.     service.sayHello();  
    6.     context.close();  
    7. }  
posted @ 2016-08-12 11:03  牧之丨  阅读(2263)  评论(0编辑  收藏  举报