1

so several articles describe how to configure spring.profiles.active inside a contextInitializerClass. You'd add something like the below to web.xml:

<context-param>
    <param-name>contextInitializerClasses</param-name>
    <param-value>com.mycompany.SpringProfileInitializer</param-value>
</context-param>

And the corresponding class:

public class SpringProfileInitializer implements       ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {

        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        environment.setActiveProfiles( getActiveProfiles(NEED_PARAM_HERE) );            
    }

}

But i need to get an argument into the call to getActiveProfiles() above; just a string will do, as long as it's "somewhere" in the web.xml file.

How can i do this?

Thx.

EDIT: Just to back up a step, the problem i'm really trying to solve is the notion of multiple wars running in one JVM, each wanting a different spring.profiles.active property, AND, the value of spring.profiles.active is going to be env-specific, sitting inside some property file.

Clearly, i can't use -Dspring.active.profiles, since this is global to the JVM. We already have a per-war properties file that is env-specific, each war knows how to pull this prop file in by using a PropertyPlaceholderConfigurer. Adding the spring.profiles.active property to this prop file doesn't work (presumably it's too late in the process). Note that these war-specific prop files are all installed in some known location on disk, with some war-specific name.

Now, i can solve my problem in 2 ways using ApplicationContextInitializer as described above.

1) I can simply have a new class per war that implements ApplicationContextInitializer, likely extending some "base" class that does the real work. The class-per-war will simply hardcode the name of the prop file, read it in, and pull out the spring.profiles.active property.

2) I can create a war-specific prop file with the same name across wars, call it spring-profile-loc.properties. This file will contain just one property, the name of the aforementioned war-specific prop file. In this scenario, there is just the SpringProfileInitializer class i have above, and it just reads in this spring-profile-loc.properties file, pulls out the single prop, and then reads in the real prop file to access the spring.profiles.active prop (there are other variants of this idea as well).

I'm not fond of either approach, they seem clunky and roundabout. It would be simpler if i can just "configure" something in the web.xml and access it directly in my impl of ApplicationContextInitializer.

But if there's some completely different approach to solving this, i'm all ears.

Thx.

Jack
  • 2,206
  • 3
  • 18
  • 25

2 Answers2

0

you can try to implement ServletContextAware and then use servletContext.getInitParameter() (or getAttribute() ?)

but i don't know if getting servletContext injected at this point works

marco.eig
  • 4,209
  • 2
  • 18
  • 26
0

You can activate profiles by this in the web.xml file:

In your Dispatcher Servlet's init-param:

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>myenv</param-value>
    </init-param>
</servlet>

Or per this

<context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>profileName</param-value>
</context-param>
Community
  • 1
  • 1
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • Yes, but that doesn't do it for me. Since the value of spring.profiles.active is per war AND per env. I can't hardcode the spring profiles in web.xml. And i don't think there's a way to specify a dynamic property inside web.xml. – Jack Sep 12 '12 at 00:37