Spring ApplicationContext - resource leak: context never closes

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

ApplicationContext context = new ClassPathXmlApplicationContext("META-INF/userLibrary.xml"); service = context.getBean(UserLibrary.class); 

UserLibrary is a third-party utility that I use in my application. The above code generates a warning for the variable "context". The warning is shown below:

 Resource leak: 'context' is never closed 

I do not understand the warning. Since the application is a spring MVC application, I cannot really close / destroy the context as I access the service while the application is running. What exactly is the warning trying to tell me?

+72
java spring eclipse spring-mvc
Jan 06 '13 at 16:17
source share
12 answers

Since the application context is a ResourceLoader (i.e., I / O), it consumes resources that need to be freed up at some point. It is also an AbstractApplicationContext extension that implements Closable . Thus, he got the close() method and can be used in the try-with-resources statement .

 try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/userLibrary.xml")) { service = context.getBean(UserLibrary.class); } 

If you really need to create this context, this is another question (you contacted it), I will not comment on this.

It is true that the context is closed implicitly when the application is stopped, but this is not enough. Eclipse is right, you need to take measures to close it manually for other cases, in order to avoid loader leaks.

+67
Jan 06 '13 at 22:21
source share

close() not defined in the ApplicationContext interface.

The only way to get rid of the warning safely:

 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(...); try { [...] } finally { ctx.close(); } 

Or, in Java 7

 try(ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(...)) { [...] } 

The main difference is that since you instantiate the context explicitly (i.e. using new ), you know the class you are creating, so you can define your variable accordingly.

If you did not create an instance of AppContext (i.e. using the one provided by Spring), you cannot close it.

+35
Feb 12 '13 at 10:16
source share

Simple casting solves the problem:

 ((ClassPathXmlApplicationContext) fac).close(); 
+10
Mar 19 '14 at 17:48
source share

Even I had the same warning, all I did was declare ApplicationContext outside the main function as private static and ta-da, the problem is fixed.

 public class MainApp { private static ApplicationContext context; public static void main(String[] args) { context = new ClassPathXmlApplicationContext("Beans.xml"); HelloWorld objA = (HelloWorld) context.getBean("helloWorld"); objA.setMessage("I'm object A"); objA.getMessage(); HelloWorld objB = (HelloWorld) context.getBean("helloWorld"); objB.getMessage(); } } 
+4
Mar 13 '14 at 5:34
source share

In the context of the application, there is an instance of the ClassPa class thanksmlApplicationContext, and the same close () method. I would just cast the appContext object and call the close () method as shown below.

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

This will clear the resource leak warning.

+4
Jan 09 '15 at 13:40
source share

try it. you need to apply fill to close applicationcontext.

  ClassPathXmlApplicationContext ctx = null; try { ctx = new ClassPathXmlApplicationContext(...); [...] } finally { if (ctx != null) ((AbstractApplicationContext) ctx).close(); } 
+3
Aug 04 '13 at 6:07 on
source share
 Object obj = context.getBean("bean"); if(bean instanceof Bean) { Bean bean = (Bean) obj; } 

In my case, the leak disappears

+1
Apr 20 '16 at 11:19
source share

Drag the context to ConfigurableApplicationContext.

 ((ConfigurableApplicationContext)context).close(); 
0
Aug 07 '14 at 9:26
source share

If you are using ClassPa thanksmlApplicationContext you can use

 ((ClassPathXmlApplicationContext) context).close(); 

to close the resource leak problem.

If you are using AbstractApplicationContext , you can use this with the close method.

 ((AbstractApplicationContext) context).close(); 

It depends on the type of context used in the application.

0
Aug 09 '15 at 21:05
source share
 import org.springframework.context.ConfigurableApplicationContext; ((ConfigurableApplicationContext)ctx).close(); 
0
Oct 02 '16 at 9:59
source share

You set the context of a static variable, which means that the context is accessible to all static methods in the class and is not limited to the scope of the main method. Therefore, the tool cannot assume that it should be closed at the end of the method anymore, so it no longer issues a warning.

 public class MainApp { private static ApplicationContext context; public static void main(String[] args) { context = new ClassPathXmlApplicationContext("Beans.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); obj.getMessage(); } } 
0
Jan 30 '17 at 18:45
source share

A method closure has been added to the ConfigurableApplicationContext interface, so best of all you can access it:

 ConfigurableApplicationContext context = new ClassPathXmlApplicationContext( "/app-context.xml"); // Use the context... context.close(); 
-one
Apr 22 '15 at 18:50
source share



All Articles