How to set active spring 3.1 environment profile through properites file, and not through env variable or system property

We use the new spring 3.1 environment profile feature. We are currently setting the active profile by setting the environment variable spring.profiles.active = xxxxx on the server on which we are deploying the application.

We believe that this is not an optimal solution, since the war file that we want to deploy should only have an additional properties file that sets the environment in which the spring application context should be loaded, so the deployment does not depend on some env var set on the server.

I tried to figure out how to do this and found:

ConfigurableEnvironment.setActiveProfiles()

which I can use to programmatically configure the profile, but then I still don’t know where and when to execute this code. Somewhere where the spring context is loading? Can I load the parameter that I want to pass to the method from the properties file?

UPDATE: I just found in docs that I could implement to set the active profile?

+48
spring environment profiles
Dec 21 '11 at 9:21
source share
4 answers

The answer from Thomasz is valid as long as the profile name can be provided statically in the web.xml file or a new type of configuration is used without XML code, where you can programmatically load the profile for installation from the properties file.

Since we are still using the XML version that I explored further, and found the following beautiful solution, in which you implement your own ApplicationContextInitializer , where you simply add a new PropertySource with a properties file to the list of sources to search for environment-specific configuration settings in In the example below, you can set the spring.profiles.active property in the spring.profiles.active file.

 public class P13nApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { private static Logger LOG = LoggerFactory.getLogger(P13nApplicationContextInitializer.class); @Override public void initialize(ConfigurableApplicationContext applicationContext) { ConfigurableEnvironment environment = applicationContext.getEnvironment(); try { environment.getPropertySources().addFirst(new ResourcePropertySource("classpath:env.properties")); LOG.info("env.properties loaded"); } catch (IOException e) { // it ok if the file is not there. we will just log that info. LOG.info("didn't find env.properties in classpath so not loading it in the AppContextInitialized"); } } } 

Then you need to add this initializer as a parameter to the ContextLoaderListener of spring, as shown below, in your web.xml :

 <context-param> <param-name>contextInitializerClasses</param-name> <param-value>somepackage.P13nApplicationContextInitializer</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 

You can also apply it to a DispatcherServlet :

 <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextInitializerClasses</param-name> <param-value>somepackage.P13nApplicationContextInitializer</param-value> </init-param> </servlet> 
+38
Dec 22
source share

In web.xml

 <context-param> <param-name>spring.profiles.active</param-name> <param-value>profileName</param-value> </context-param> 

Using WebApplicationInitializer

This approach is used when you do not have a web.xml in Servlet 3.0 and Spring is fully loaded from Java:

 class SpringInitializer extends WebApplicationInitializer { void onStartup(ServletContext container) { AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.getEnvironment().setActiveProfiles("profileName"); rootContext.register(SpringConfiguration.class); container.addListener(new ContextLoaderListener(rootContext)); } } 

Where the SpringConfiguration class is annotated using @Configuration .

+50
Dec 21 '11 at 9:40
source share

For some reason, only one way works for me

 public class ActiveProfileConfiguration implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.setProperty(AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME, "dev"); System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev"); } 

....

  <listener> <listener-class>somepackahe.ActiveProfileConfiguration</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 
+6
Dec 26
source share

Here is a variant of the P13nApplicationContextInitializer approach. However, this time we get the path to env properties from JNDI. In my case, I set the global JNDI environment variable as coacorrect / spring -profile = file: /tmp/env.properties

  • In tomcat / tomee server.xml add this: <Environment name="coacorrect/spring-profile" type="java.lang.String" value="/opt/WebSphere/props"/>
  • Next, in tomcat / tomee, add to WAR META-INF / context.xml <ResourceLink global="coacorrect/spring-profile" name="coacorrect/spring-profile" type="java.lang.String"/>
  • In any container add the appropriate one in web.xml

     public class SpringProfileApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext>{ public static final Logger log = LoggerFactory.getLogger(SpringProfileApplicationContextInitializer.class); private static final String profileJNDIName="coacorrect/spring-profile"; private static final String failsafeProfile="remote-coac-dbserver"; @Override public void initialize(ConfigurableApplicationContext applicationContext) { ConfigurableEnvironment environment = applicationContext.getEnvironment(); try { InitialContext ic = new InitialContext(); Object r1 = ic.lookup(profileJNDIName); if (r1 == null) { // try the tomcat variant of JNDI lookups in case we are on tomcat/tomee r1 = ic.lookup("java:comp/env/"+profileJNDIName); } if (r1 == null) { log.error("Unable to locate JNDI environment variable {}", profileJNDIName); return; } String profilePath=(String)r1; log.debug("Found JNDI env variable {} = {}",r1); environment.getPropertySources().addFirst(new ResourcePropertySource(profilePath.trim())); log.debug("Loaded COAC dbprofile path. Profiles defined {} ", Arrays.asList(environment.getDefaultProfiles())); } catch (IOException e) { // it ok if the file is not there. we will just log that info. log.warn("Could not load spring-profile, defaulting to {} spring profile",failsafeProfile); environment.setDefaultProfiles(failsafeProfile); } catch (NamingException ne) { log.error("Could not locate JNDI variable {}, defaulting to {} spring profile.",profileJNDIName,failsafeProfile); environment.setDefaultProfiles(failsafeProfile); } } 

    }

0
Jun 13 '15 at 3:50
source share



All Articles