Override Default WebApplicationContext Loading

The default initialization procedure of Spring's WebApplicationContext requires a context-param named contextConfigLocation in the application's web.xml.

This parameter is available as an initParam of the ServletContext at runtime. Unfortunately these cannot be set at runtime.

Setting them at buildtime was not an option because we are using Maven2. As we speak the war plugin of Maven does not enable proper filtering.

We built a workaround by letting our statup listener build the spring application context, reading the environment specific settings for a properties file.

// Create spring webapplicationcontext - replaces
// the functionality of ContextLoaderListener
XmlWebApplicationContext webApplicationContext = 
    new XmlWebApplicationContext();

// Config locations can be found in spring-deploy.properties
Properties properties = new Properties();

try {
    properties.load(getClass().getResourceAsStream(
            "/spring-deploy.properties"));
} catch (IOException e) {
    log.error(e.getMessage());
}

webApplicationContext.setConfigLocations(
    StringUtils.tokenizeToStringArray(
            properties.getProperty("spring.contextconfiglocation"), ","));

webApplicationContext.refresh();

servletContext.setAttribute(
        WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, 
        applicationContext);
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.