Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get a list of all the Spring application contexts running in the JVM?

I would like to be able to get a list of all Spring application contexts from the web applications running in my servlet container. This so so that I can re-initialise the contexts without restarting or reloading my container or webapps.

Is this possible? If so, how? Is there anyway to get a list of all the servlet contexts running on a servlet container instance through the Servlet API?

like image 509
Ricardo Gladwell Avatar asked Feb 16 '09 14:02

Ricardo Gladwell


2 Answers

No - to both questions.

Access to all servlet contexts would be a security problem. You would be able to inspect and/or manipulate other webapps. The only one knowing all servlet contexts is the container itself. But this is internal implementation, there is no api.

Spring context access is similar, but if you mean all spring contexts in one webapp, they will create a hierarchy - those for controllers for example. Implementing an org.springframework.context.ApplicationListener as bean in the root spring context (as initialized by the org.springframework.web.context.ContextLoaderListener configured in the web.xml) can notify you about contexts started, stopped, closed or refreshed.

like image 152
Arne Burmeister Avatar answered Oct 01 '22 15:10

Arne Burmeister


[Edit: This doesn't really help, as each web.xml would have to have the same listener]

You could have a ServletContextAttributeListener listen out for insertions of the standard WebApplicationContext attribute, and then notify some centralised monitor?

Not tested:

public class SpringWACListener implements ServletContextAttributeListener {
    public void attributeAdded(ServletContextAttributeEvent scab) {
        if (WebApplicationContext.
            ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE.
            equals(scab.getName()) {
            myCentralisedMonitor.notify("added");
        }
    }
    // same for others
}
like image 21
toolkit Avatar answered Oct 01 '22 13:10

toolkit