Spring ApplicationContext - Resource leak: 'context' is never closed

前端 未结 17 573
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 17:15

In a spring MVC application, I initialize a variable in one of the service classes using the following approach:

ApplicationContext context = 
         new C         


        
17条回答
  •  無奈伤痛
    2020-12-04 17:51

    As the Application context has an instance of ClassPathXmlApplicationContext and the same has a close() method. I would simply CAST the appContext object and invoke the close() method as below.

    ApplicationContext appContext = new ClassPathXmlApplicationContext("spring.xml");
    //do some logic
    ((ClassPathXmlApplicationContext) appContext).close();
    

    This will fix the Resource Leak warning.

提交回复
热议问题