In a spring MVC application, I initialize a variable in one of the service classes using the following approach:
ApplicationContext context =
new C
Object obj = context.getBean("bean");
if(bean instanceof Bean) {
Bean bean = (Bean) obj;
}
In my case leak disappears
A simple cast solves the issue:
((ClassPathXmlApplicationContext) fac).close();
Downcast the context to ConfigurableApplicationContext.
((ConfigurableApplicationContext)context).close();
for ApplicationContext you can't use close() method because this method doesn't define int it but instead you can use:(ClassPathXmlApplicationContext(context)).close()
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.
This worked out best for me.
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
private static ApplicationContext con;
public static void main(String[] args) {
con = new ClassPathXmlApplicationContext("config.xml");
Employee ob = (Employee) con.getBean("obj");
System.out.println("Emp Id " + ob.getEmpno());
System.out.println("Emp name " + ob.getEmpname());
}
}