How to add active spring profile from environment variable?

So far, I have set the following environment variable to ~/.bash_profile :

 export SPRING_PROFILES_ACTIVE=local 

This parameter sets my active spring profile. But now I want to add the local profile to other profiles defined in application.properties , and not replace them.

Spring's Download Documentation has a section on adding an active profile, but I don't see anything about adding an active profile from an environment variable.

I tried to set the environment variable SPRING_PROFILES_INCLUDE , but this has no effect.

How to do it?

PS: I am using spring boot 1.4.2.

+6
source share
4 answers

With default add profile

You can enter your own environment variable in the application.properties file, next to specific profiles, using an expression. For example, if your current file looks like this:

 spring.profiles.active=profile1,profile2 

with a user environment variable, it will change to:

 spring.profiles.active=profile1,profile2,${ADDITIONAL_APP_PROFILES:local} 

where ADDITIONAL_APP_PROFILES is the name of the environment variable that you set instead of SPRING_PROFILES_ACTIVE .

The local value is used when the variable is not set in the current environment. In this case, a profile called local will be activated. If you do not specify a default value and there is no environment variable, the entire expression will be used as the name of the active profile.

No default add profile

If you like not to activate the default profile, you can remove the placeholder value and comma before the variable expression:

 spring.profiles.active=profile1,profile2${ADDITIONAL_APP_PROFILES} 

but in this case, the variable set in the current environment should begin with a comma:

 export ADDITIONAL_APP_PROFILES=,local 
+8
source

The following sentence in the documentation you are attached to:

It is sometimes useful to have profile-specific properties that add to active profiles rather than replace them. The spring.profiles.include property can be used to unconditionally add active profiles.

So, you can run the application with a command line parameter:

 -Dspring.profiles.include=${SPRING_PROFILES_INCLUDE} 
+2
source

This is an example of adding a programmatically additional active profile from the system env or jvm arg.

 @Configuration public class ApplicationInitializer implements WebApplicationInitializer, ApplicationContextInitializer<ConfigurableWebApplicationContext> { @Override public void onStartup(ServletContext servletContext) throws ServletException { servletContext.setInitParameter("contextInitializerClasses", this.getClass().getCanonicalName()); } @Override public void initialize(ConfigurableWebApplicationContext applicationContext) { ConfigurableEnvironment environment = applicationContext.getEnvironment(); environment.addActiveProfile(System.getProperty("myProperty")); environment.addActiveProfile(System.getEnv("myProperty")); } } 
0
source

To support the bash environment, the available values SPRING_PROFILES_ACTIVE and SPRING_PROFILES_DEFAULT

no, SPRING_PROFILES_INCLUDE

you may have to resort to the command line -Dspring.profiles.include or program your workout with ConfigurableEnvironment

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/env/AbstractEnvironment.html#ACTIVE_PROFILES_PROPERTY_NAME

-1
source

Source: https://habr.com/ru/post/1259847/


All Articles