如何从系统变量设置Spring配置文件?

use*_*851 45 java spring spring-profiles

我有一个使用另一个项目的Spring项目.每个项目都有自己的弹簧配置文件,使用applicationContext.xml*.properties为每个配置文件初始化java代码.我从中注入了个人资料args[].问题是第二个项目使用env的默认配置, applicationContext.xml 我不能将env注入args[]第二个项目,我试着找一篇文章来解释Spring profile的工作原理.

  1. 是否存在一个层次结构,当未配置默认值时,它将查看配置文件applicationContext.xml
  2. System var是否比applicationContext.xml配置更强大?
  3. 您认为什么是我挑战的最佳解决方案?

关于该主题甚至实例的文章将非常感谢!! 提前致谢.

Lal*_*Jha 81

SPRING_PROFILES_ACTIVE是覆盖/选择Spring配置文件的环境变量

  • 这也适用于Docker. (4认同)
  • 参考:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-set-active-spring-profiles (3认同)

Jan*_*Gun 43

如果您为JVM提供Spring配置文件,那么应该没有问题:

java -Dspring.profiles.active=development -jar yourApplication.jar 
Run Code Online (Sandbox Code Playgroud)

另见Spring-Documentation:

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html

69.5设置活动的弹簧配置文件

Spring Environment有一个API,但通常你会设置一个System属性(spring.profiles.active)或一个OS环境变量(SPRING_PROFILES_ACTIVE).例如,使用-D参数启动应用程序(记得将它放在主类或jar存档之前):

$ java -jar -Dspring.profiles.active = production demo-0.0.1-SNAPSHOT.jar

在Spring Boot中,您还可以在application.properties中设置活动配置文件,例如

spring.profiles.active =生产

以这种方式设置的值将由System属性或环境变量设置替换,但不会由SpringApplicationBuilder.profiles()方法替换.因此,后一个Java API可用于扩充配置文件而不更改默认值.

有关详细信息,请参见"Spring Boot功能"一节中的第25章"配置文件".


Ajm*_*yar 26

我通常使用基于注释的配置而不是基于XML的配置来配置applicationContext .无论如何,我相信他们两个都有同样的优先权.

*回答你的问题,系统变量具有更高的优先级*


从applicationContext获取基于配置文件的bean

  • 在Bean上使用@Profile

    @Component
    @Profile("dev")
    public class DatasourceConfigForDev
    
    Run Code Online (Sandbox Code Playgroud)

    现在,个人资料是 dev

    注意:如果给出@Profile("!dev")配置文件, 那么配置文件将排除dev并且适用于所有其他配置文件.

  • 在XML中使用profiles属性

    <beans profile="dev">
      <bean id="DatasourceConfigForDev" class="org.skoolguy.profiles.DatasourceConfigForDev"/>
    </beans>
    
    Run Code Online (Sandbox Code Playgroud)

设置配置文件的值:

  • 以编程方式通过WebApplicationInitializer接口

    在Web应用程序中,WebApplicationInitializer可用于以编程方式配置ServletContext

    @Configuration
    public class MyWebApplicationInitializer implements WebApplicationInitializer {
    
      @Override
      public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.setInitParameter("spring.profiles.active", "dev");
      }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 通过ConfigurableEnvironment以编程方式

    您还可以直接在环境中设置配置文件:

    @Autowired
    private ConfigurableEnvironment env;
    
    ...
    
    env.setActiveProfiles("dev");
    
    Run Code Online (Sandbox Code Playgroud)
  • web.xml中的Context参数

    也可以使用context参数在Web应用程序的web.xml中激活配置文件:

    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/app-config.xml</param-value>
    </context-param>
    <context-param>
      <param-name>spring.profiles.active</param-name>
      <param-value>dev</param-value>
    </context-param>
    
    Run Code Online (Sandbox Code Playgroud)
  • JVM系统参数

    作为参数传递的配置文件名称将在应用程序启动期间激活:

    -Dspring.profiles.active=dev
    
    Run Code Online (Sandbox Code Playgroud)

    在IDE中,您可以设置应用程序运行时要使用的环境变量和值.以下是Eclipse中的运行配置:

Eclipse Run Configuration  - 截图不可用

  • 环境变量

    通过命令行设置: export spring_profiles_active=dev

任何未指定配置文件的bean都属于"默认"配置文件.


优先顺序是:

  1. web.xml中的Context参数
  2. WebApplicationInitializer
  3. JVM系统参数
  4. 环境变量


db8*_*b80 7

如果您使用 docker 部署 spring boot 应用程序,则可以使用标志e设置配置文件

docker run -e "SPRING_PROFILES_ACTIVE=prod" -p 8080:8080 -t r.test.co/myapp:latest


Lav*_*ish 7

您可以通过提供来设置弹簧轮廓-Dspring.profiles.active=<env>

对于源(src)目录中的java文件,您可以使用 System.getProperty("spring.profiles.active")

对于测试目录中的 java 文件,您可以提供

  • SPRING_PROFILES_ACTIVE<env>

或者

因为“test”任务会忽略“environment”、“jvmArgs”和“systemProperties”。在 root 中build.gradle添加一个任务来设置 jvm 属性和环境变量。

test {
    def profile = System.properties["spring.profiles.active"]
    systemProperty "spring.profiles.active",profile
    environment "SPRING.PROFILES_ACTIVE", profile
    println "Running ${project} tests with profile: ${profile}"
}
Run Code Online (Sandbox Code Playgroud)