1

I'm migrating a web app to Spring 3.2 and am enjoying the web.xml-free configuration. One part that's remaining is setting the webapp root key, which I previously did in web.xml like so:

<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapproot</param-value>
</context-param>

I know that Spring creates a default key, but in my case I'm running multiple versions of the same war and need to set the key to a different value in each. So optimally I'd like to take a value from a properties file and use that as the rootkey.

I imagine that I would do this somewhere here:

public class WebAppInitializer implements WebApplicationInitializer {
    private static final Logger logger = Logger.getLogger(WebAppInitializer.class);

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    // Create the root appcontext
       AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
       rootContext.register(AppConfig.class);

       servletContext.addListener(new WebAppRootListener());

    // Manage the lifecycle of the root appcontext
       servletContext.addListener(new ContextLoaderListener(rootContext));
       //servletContext.setInitParameter("defaultHtmlEscape", "true");

    // The main Spring MVC servlet.
       ServletRegistration.Dynamic springapp = servletContext.addServlet(
          "springapp", new DispatcherServlet(rootContext));
            springapp.setLoadOnStartup(1);
       Set<String> mappingConflicts = springapp.addMapping("/");
...etc...

Thanks to anyone who can offer advice!

bz3x
  • 171
  • 4
  • 15

1 Answers1

1

The first part was simple:

servletContext.setInitParameter("webAppRootKey", getRootKey());

and to get the rootkey, which in this case is added to the application.properties file by the maven build,

private String getRootKey() {
    Properties prop = new Properties();
    ClassLoader loader = Thread.currentThread().getContextClassLoader();           
    InputStream stream = loader.getResourceAsStream("application.properties");
    String key=null;
    try {
        prop.load(stream);
        key = prop.getProperty("rootKey");
    } catch  (Exception e) {
        throw new RuntimeException("Cannot load webapprootkey", e);
    }
    return key;
}
bz3x
  • 171
  • 4
  • 15
  • can you share what is in application.properties, I have a scenario where running two webapps that are created from a same maven quickstart and causing problem if add both apps at the smae time on server. Here is my question:http://stackoverflow.com/q/18543317/1654823 , Could you please have a look – Venkat Sep 12 '13 at 01:36