Reputation: 1016
Is it possible to load springs application context only on demand? Scenario is as following: I have a web application with a setup page for the user to enter database properties. Those are stored within a properties file and loaded with springs PropertyPlaceHolderConfigurer into the application-context.xml when restarting after setup.
However, if I do not have initial valid database information, spring fails to start. So, the setup page won't show up. Is there any clean way to prevent the context from being loaded?
One solution that comes to my mind would be to remove the context listener in web.xml and write it back after setup, however this is rather a nasty hack than a clean solution.
Upvotes: 0
Views: 557
Reputation: 1016
After thinking a while about this, i'll go with the following approach:
Leaving the web.xml alone and just replacing the spring classes with clients delegating to them. So in my case this would be a
protected class OnDemandContextLoaderListener extends ContextLoaderListener {
@Override
public void contextInitialized(ServletContextEvent event) {
// some logic to find out if properly setup
// if so:
super.contextInitialized(event):
}
...
}
Should also work with the DispatcherServlet as well.
Upvotes: 0
Reputation: 340923
Just... don't start it. I guess you are bootstrapping Spring from web.xml
. Don't put ContextLoaderListener
there (or put some minimal context to barely display setup page).
Unfortunately once the setup is over, it's up to you to start (and stop) Spring context. It's pretty simple, you can start Spring from code e.g. using ClassPathXmlApplicationContext
.
Upvotes: 3