How actually applicationServlet manages context varibles ? If I set variables in applicationContext level whether it stores in Method area or stack area or heap , if so how it stored and how its accessed ?
Here i created local variable as MAP and put some values in applicationContext level.
I wrote some sample code below here.
public class ContextLoaderListener implements ServletContextListener {
private ServletContext ctx;
private WebApplicationContext springContext;
@Override
public void contextInitialized(ServletContextEvent sce) {
Map<String, String> resourceMap=new HashMap<>();
resourceMap.put("1","abc1");
resourceMap.put("2","abc2");
resourceMap.put("3","abc3");
sce.getServletContext().setAttribute("resourceMap", resourceMap);
}
@Override
public void contextDestroyed(ServletContextEvent sce) { }
}
I am going to answer your comment here, I think it answers your question as well.
Your question in the comment is as follows:
Well resource map is just a local reference. The object itself in the heap. You are right that resourceMap gets lost after the method execution completes. However you are passing a copy of the reference to another object which lives longer. See the following example:
Does this answer your question?