【问题标题】:Setting the default active profile in Spring-boot在 Spring-boot 中设置默认活动配置文件
【发布时间】:2016-10-08 14:16:43
【问题描述】:

如果未设置 -Dspring.profiles.active,我希望我的默认活动配置文件为 production

我在application.properties 中尝试了以下操作,但没有成功:

spring.profiles.default=production

Spring-boot 版本 = 1.3.5.RELEASE

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    您在此处所做的是将默认 default 配置文件(如果您未指定 @Profile 注释,则在任何 bean 上使用的配置文件)设置为 production

    您真正需要做的是设置默认的active配置文件,如下所示:

    spring.profiles.active=production
    

    【讨论】:

    • 那行得通,参数-Dspring.profiles.active=development 覆盖它,这就是我想要的
    • @PaulNUK 这里的“生产”是什么意思??
    • production 只是配置文件的名称(它可以是任何有效的配置文件)。正如最初的问题询问如何将活动配置文件设置为生产,这就是我在答案中提出的内容。
    【解决方案2】:

    添加--spring.profiles.active=production

    例子:

    java -jar file.jar --spring.profiles.active=production
    

    【讨论】:

    • 这是我的解决方案。以前,我尝试过java -jar file.jar -Dspring.profiles.active=production,但这对我不起作用。
    • 如果你对 -D args 风格感兴趣,它应该在下面 java -Dspring.profiles.active=production -jar file.jar
    • 确实,使用 -D 对我也不起作用,使用 --spring.profiles.active 作为命令行参数可以。
    【解决方案3】:

    我们在java中设置spring.profiles.active时遇到了类似的问题。

    这是我们在尝试了四种不同的提供spring.profiles.active的方式后最终得出的结论。

    java-8

    $ java --spring.profiles.active=dev -jar my-service.jar
    Gives unrecognized --spring.profiles.active option.
    
    $ java -jar my-service.jar --spring.profiles.active=dev
    # This works fine
    
    $ java -Dspring.profiles.active=dev -jar my-service.jar
    # This works fine
    
    $ java -jar my-service.jar -Dspring.profiles.active=dev
    # This doesn't works
    

    java-11

    $ java --spring.profiles.active=dev -jar my-service.jar
    Gives unrecognized --spring.profiles.active option.
    
    $ java -jar my-service.jar --spring.profiles.active=dev
    # This doesn't works
    
    $ java -Dspring.profiles.active=dev -jar my-service.jar
    # This works fine
    
    $ java -jar my-service.jar -Dspring.profiles.active=dev
    # This doesn't works
    

    注意:如果您在 application.properties 文件中指定 spring.profiles.active,请确保如上所述向 java 提供 spring.config.locationspring.config.additional-location 选项。

    【讨论】:

    • 谢谢。对我来说,java 11 就是你提到的那个:'java -Dspring.profiles.active=dev -jar my-service.jar'
    【解决方案4】:

    如果您使用的是 maven,我会这样做:

    成为生产您的默认个人资料:

    <properties>
        <activeProfile>production</activeProfile>
    </properties>
    

    作为其他配置文件的示例:

    <profiles>
        <!--Your default profile... selected if none specified-->
        <profile>
            <id>production</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <activeProfile>production</activeProfile>
            </properties>
        </profile>
    
        <!--Profile 2-->
        <profile>
            <id>development</id>
            <properties>
                <activeProfile>development</activeProfile>
            </properties>
        </profile>
    
        <!--Profile 3-->
        <profile>
            <id>otherprofile</id>
            <properties>
                <activeProfile>otherprofile</activeProfile>
            </properties>
        </profile>
    <profiles>
    

    您必须在 application.properties 中设置:

    spring.profiles.active=@activeProfile@
    

    这对我每次都有效,希望它能解决你的问题。

    【讨论】:

    • 注意 - 使用配置文件使用maven package -P{profilename} - 例如maven package -Pproduction 用于基于生产的配置文件。
    • 这是否会自动工作而无需在运行时指定配置文件,就像上面提到的 Artegon 一样。这真的可以在不从命令行传递配置文件的情况下将 activeByDefault 设置为 true 吗?对我来说,使用 activeByDefault 不起作用..
    • 知道如何用 yml 做到这一点吗?好像不行
    【解决方案5】:

    我是这样做的

        System.setProperty("spring.profiles.default", "dev");
    

    main(...)的开头

    【讨论】:

    • 就个人而言,我更喜欢引用常量org.springframework.core.env.AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME 而不是它的值。
    【解决方案6】:

    首先,通过下面的解决方案,有必要了解spring boot总是会读取application.properties文件。所以对方的配置文件只会补充和替换之前定义的属性。

    考虑以下文件:

    application.properties
    application-qa.properties
    application-prod.properties
    

    1) 非常重要。 application.properties只是这个文件,必须有以下行:

    spring.profiles.active=@spring.profiles.active@
    

    2) 在 QA 和 PROD 配置文件中更改您想要的内容,以查看环境之间的差异。

    3) 通过命令行,使用以下任何选项启动 spring boot 应用程序:

    它将使用默认的application.properties 文件启动应用程序:

    mvn spring-boot:run
    

    它将加载默认的application.properties 文件和application-qa.properties 文件之后,替换和/或补充默认配置:

    mvn spring-boot:run -Dspring.profiles.active=qa
    

    此处相同,但使用的是生产环境而不是 QA:

    mvn spring-boot:run -Dspring.profiles.active=prod
    

    【讨论】:

    • 此解决方案无需更改 Java 文件。
    【解决方案7】:

    把这个放到App.java中:

    public static void main(String[] args) throws UnknownHostException {
        SpringApplication app = new SpringApplication(App.class);
        SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args);
        if (!source.containsProperty("spring.profiles.active") &&
                !System.getenv().containsKey("SPRING_PROFILES_ACTIVE")) {
    
            app.setAdditionalProfiles("production");
        }
        ...
    }
    

    JHipster 是这样处理的

    【讨论】:

    • 以编程方式在 aws lambda 中设置配置文件。谢谢!
    【解决方案8】:

    如果您将 AWS Lambda 与 SprintBoot 一起使用,则必须在环境变量下声明以下内容:

    键:JAVA_TOOL_OPTIONS & 值:-Dspring.profiles.active=dev

    【讨论】:

      【解决方案9】:

      目前使用 Maven + Spring Boot。我们的解决方案如下:

      application.properties

      #spring.profiles.active= # comment_out or remove
      

      securityConfig.java

      @Value(${spring.profiles.active:[default_profile_name]}")
      private String ACTIVE_PROFILE_NAME;
      

      信用以MartinMlima 开头。这里提供了类似的答案:

      How do you get current active/default Environment profile programmatically in Spring?

      【讨论】:

        【解决方案10】:

        @Profile 注释中还可以有多个列表

        @Profile({"dev","default"})
        

        如果将“default”设置为附加值,则不必指定 spring.profiles.active

        【讨论】:

          【解决方案11】:

          无需每次更改源代码的简洁方法是使用操作系统环境变量SPRING_PROFILES_ACTIVE

          export SPRING_PROFILES_ACTIVE=production
          

          how-to-set-active-spring-profiles

          【讨论】:

            【解决方案12】:

            在 AWS LAMBDA 中:

            对于$ sam local,您在 sam 模板 yml 文件中添加以下行:

            Resources:
               FunctionName:
                   Properties:
                       Environment:
                           Variables:
                              SPRING_PROFILES_ACTIVE: local
            

            但在 AWS 控制台中: 在您的 Lambda 环境变量中添加:

            键:JAVA_TOOL_OPTIONS 值:-Dspring.profiles.active=dev

            【讨论】:

              【解决方案13】:

              如果正在创建 Spring Boot 应用程序,则可以根据环境拥有单独的应用程序属性文件。 例如 - 开发环境的属性文件, application-dev.properties

              spring.hivedatasource.url=<hive dev data source url>
              spring.hivedatasource.username=dev
              spring.hivedatasource.password=dev
              spring.hivedatasource.driver-class-name=org.apache.hive.jdbc.HiveDriver
              

              application-test.properties:

              spring.hivedatasource.url=<hive dev data source url>
              spring.hivedatasource.username=test
              spring.hivedatasource.password=test
              spring.hivedatasource.driver-class-name=org.apache.hive.jdbc.HiveDriver
              

              还有一个主要的 application.properties 文件来选择配置文件:

              application.properties:

              spring.profiles.active=dev
              server.tomcat.max-threads = 10
              spring.application.name=sampleApp
              

              如下定义数据库配置:

              @Configuration
              @ConfigurationProperties(prefix="spring.hivedatasource")
              public class DBConfig {
              
                  @Profile("dev")
                  @Qualifier("hivedatasource")
                  @Primary
                  @Bean
                  public DataSource devHiveDataSource() {
                      System.out.println("DataSource bean created for Dev");
                      return new BasicDataSource();
                  }
              
                  @Profile("test")
                  @Qualifier("hivedatasource")
                  @Primary
                  @Bean
                  public DataSource testHiveDataSource() {
                      System.out.println("DataSource bean created for Test");
                      return new BasicDataSource();
                  }
              

              这将根据 application.properties 文件中设置的活动配置文件自动创建 BasicDataSource bean。 运行 Spring-boot 应用程序并进行测试。

              请注意,这将在调用 getConnection() 之前创建一个空 bean。 一旦连接可用,您就可以使用该 DataSource bean 获取 url、驱动程序类等。

              【讨论】:

              • 应用程序属性配置文件的美妙之处在于您可以拥有多个互斥的属性集。所以你不需要定义两个bean。只需定义一个不带@Profile 的对象,它就会获得与活动配置文件对应的属性。
              【解决方案14】:

              试试这个: @PropertySource("classpath:${spring.profiles.active:production}_file.properties")

              【讨论】:

                【解决方案15】:

                如果您在配置中使用 application.yml,请将其添加到其中,以设置默认的活动配置文件:

                spring:
                  profiles:
                    active: production
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2021-02-01
                  • 2018-11-12
                  • 1970-01-01
                  • 2015-09-11
                  • 2020-12-12
                  • 1970-01-01
                  • 2019-10-27
                  相关资源
                  最近更新 更多