Question

I need to modify the web.xml in my project after checking some parameters. My contextConfigLocation parameter in web.xml is as follows:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
  classpath:*/test-spring-config.xml classpath:*/someContext.xml classpath:applicationContext.xml classpath:databaseContext.xml</param-value>
</context-param>

Now I need to add classpath:securityContext.xml to my contextConfigLocation in web.xml upon checking some condition before my application is loaded in tomcat. I've tried doing this in my applicationInitializer class which implements ServletContextListener as follows(part of the code is shown):

public class ApplicationInitializer implements ServletContextListener
{ 
   public void contextInitialized(ServletContextEvent event)
{ ServletContext sc = event.getServletContext();
      if(someConditionIsTrue){
        XmlWebApplicationContext appContext = new XmlWebApplicationContext();
        appContext.setConfigLocation("classpath:securityContext.xml");
        appContext.setServletContext(sc);
        appContext.refresh();
        sc.setAttribute("org.springframework.web.context.WebApplicationContext.ROOT",appContext);
}

But this is not working because I'm again loading the context in my web.xml as

<listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Can somebody suggest how to deal with this problem? Any suggestions are appreciated.

Was it helpful?

Solution

ApplicationInitializer should extend ContextLoaderListener for your scenario and listener class should be pointing to ApplicationInitializer in web.xml

OTHER TIPS

After a few days of struggle, I was able to do this by creating a CustomContextLoaderListener class which extends ContextLoaderListener and override its createContextLoader method which now returns a customContextLoader as follows:

@Override
protected ContextLoader createContextLoader(){
return new CustomContextLoader();
}

The CustomContextLoader class extends ContextLoader and I've override its customizeContext method as follows:

@Override
protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext){
super.customizeContext(servletContext, applicationContext);
String[] oldLocations = applicationContext.getConfigLocations();
String[] newLocations;
if(conditionIsTrue){
newLocations = new String[oldLocations.length+1];
for (int i = 0; i < oldLocations.length; i++) {
        newLocations[i] = oldLocations[i];
    }
newLocations[oldLocations.length] ="classpath:securityContext.xml";
}else{
    newLocations = new String[oldLocations.length];
    newLocations = Arrays.copyOf(oldLocations, oldLocations.length);
}
applicationContext.setConfigLocations(newLocations);
}

Do not forget to add the customContextLoaderListener class in your web.xml.

<listener>
<listener-class>com.abc.px.customContextLoaderListener</listener-class>
</listener>

This is it! This way I can conditionally set my context in web.xml. Hope this helps someone!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top