如何从环境变量中添加活动的 spring 配置文件?
How to add an active spring profile from an environment variable?
到目前为止,我在 ~/.bash_profile
中设置了以下环境变量:
export SPRING_PROFILES_ACTIVE=local
这设置了我的活动 spring 配置文件。但是现在,我想 add 本地配置文件到 application.properties
中定义的其他配置文件,而不是 replace 它们。
在 Spring Boot documentation 中,有一个关于添加活动配置文件的部分,但我没有看到任何有关从环境变量添加活动配置文件的内容。
我尝试设置 SPRING_PROFILES_INCLUDE
环境变量,但这没有效果。
如何操作?
P.S.: 我正在使用 Spring Boot 1.4.2.
使用默认添加配置文件
您可以在 application.properties
文件中使用表达式在定义的配置文件旁边引入您自己的环境变量。例如,如果您当前的文件如下所示:
spring.profiles.active=profile1,profile2
使用自定义环境变量它将变为:
spring.profiles.active=profile1,profile2,${ADDITIONAL_APP_PROFILES:local}
其中 ADDITIONAL_APP_PROFILES
是您设置的环境变量的名称,而不是 SPRING_PROFILES_ACTIVE
。
值local
在当前环境中未设置变量时使用。在这种情况下,名为 local
的配置文件将被激活。如果您不设置默认值并且环境变量不存在,整个表达式将用作活动配置文件的名称。
没有默认添加配置文件
如果您想避免激活默认配置文件,您可以删除变量表达式前的占位符值和逗号:
spring.profiles.active=profile1,profile2${ADDITIONAL_APP_PROFILES}
但在那种情况下,当前环境中设置的变量必须以逗号开头:
export ADDITIONAL_APP_PROFILES=,local
要支持 bash 环境,可用值为 SPRING_PROFILES_ACTIVE 和 SPRING_PROFILES_DEFAULT
不是,SPRING_PROFILES_INCLUDE
您可能必须使用命令行方式 -Dspring.profiles.include 或使用 ConfigurableEnvironment
以编程方式锻炼
您链接到的文档中的下一句:
Sometimes it is useful to have profile-specific properties that add to the active profiles rather than replace them. The spring.profiles.include
property can be used to unconditionally add active profiles.
因此您可以使用命令行参数启动您的应用程序:
-Dspring.profiles.include=${SPRING_PROFILES_INCLUDE}
这是添加来自系统环境或 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"));
}
}
到目前为止,我在 ~/.bash_profile
中设置了以下环境变量:
export SPRING_PROFILES_ACTIVE=local
这设置了我的活动 spring 配置文件。但是现在,我想 add 本地配置文件到 application.properties
中定义的其他配置文件,而不是 replace 它们。
在 Spring Boot documentation 中,有一个关于添加活动配置文件的部分,但我没有看到任何有关从环境变量添加活动配置文件的内容。
我尝试设置 SPRING_PROFILES_INCLUDE
环境变量,但这没有效果。
如何操作?
P.S.: 我正在使用 Spring Boot 1.4.2.
使用默认添加配置文件
您可以在 application.properties
文件中使用表达式在定义的配置文件旁边引入您自己的环境变量。例如,如果您当前的文件如下所示:
spring.profiles.active=profile1,profile2
使用自定义环境变量它将变为:
spring.profiles.active=profile1,profile2,${ADDITIONAL_APP_PROFILES:local}
其中 ADDITIONAL_APP_PROFILES
是您设置的环境变量的名称,而不是 SPRING_PROFILES_ACTIVE
。
值local
在当前环境中未设置变量时使用。在这种情况下,名为 local
的配置文件将被激活。如果您不设置默认值并且环境变量不存在,整个表达式将用作活动配置文件的名称。
没有默认添加配置文件
如果您想避免激活默认配置文件,您可以删除变量表达式前的占位符值和逗号:
spring.profiles.active=profile1,profile2${ADDITIONAL_APP_PROFILES}
但在那种情况下,当前环境中设置的变量必须以逗号开头:
export ADDITIONAL_APP_PROFILES=,local
要支持 bash 环境,可用值为 SPRING_PROFILES_ACTIVE 和 SPRING_PROFILES_DEFAULT
不是,SPRING_PROFILES_INCLUDE
您可能必须使用命令行方式 -Dspring.profiles.include 或使用 ConfigurableEnvironment
以编程方式锻炼您链接到的文档中的下一句:
Sometimes it is useful to have profile-specific properties that add to the active profiles rather than replace them. The
spring.profiles.include
property can be used to unconditionally add active profiles.
因此您可以使用命令行参数启动您的应用程序:
-Dspring.profiles.include=${SPRING_PROFILES_INCLUDE}
这是添加来自系统环境或 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"));
}
}