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?
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.
[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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With