【问题标题】:how to use Spring Boot profiles如何使用 Spring Boot 配置文件
【发布时间】:2017-02-24 22:30:52
【问题描述】:

我有application.yml,application-dev.ymlapplication-dev.yml

  1. 我正在使用 maven 命令 mvn spring-boot:run -Dspring.profiles.active=dev 它不起作用,我无法使用 mvn spring-boot:run 选择开发配置文件。我该如何选择?
  2. 文档说java -jar XXX.jar --spring.profiles.active=dev 有效,我尝试了-Dspring.profiles.active=dev,但它不起作用。在我的项目中,我使用java -jar XXX.jar 它运行,但如果我使用java -jar XXX.jar --spring.profiles.active=dev 选择开发配置文件,控制台会打印这么多日志并警告我从未见过使用java -jar XXX.jar,并告诉我APPLICATION FAILED TO START

那么如何解决两个问题呢?谢谢~

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    我不确定我是否完全理解这个问题,但我会尝试通过提供有关 Spring Boot 中的配置文件的一些详细信息来回答。

    对于您的 #1 示例,根据文档,您可以使用 -Drun.profiles 使用 Spring Boot Maven 插件选择配置文件。

    编辑:对于 Spring Boot 2.0+,run 已重命名为 spring-boot.runrun.profiles 已重命名为 spring-boot.run.profiles

    mvn spring-boot:run -Dspring-boot.run.profiles=dev
    

    https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/maven-plugin/examples/run-profiles.html

    从您的#2 示例中,您在 jar 名称之后定义活动配置文件。您需要在您正在运行的 jar 名称之前提供 JVM 参数。

    java -jar -Dspring.profiles.active=dev XXX.jar
    

    一般信息:

    您提到您同时拥有application.ymlapplication-dev.yml。使用dev 配置文件运行实际上会加载both 配置文件。来自application-dev.yml 的值将覆盖application.yml 提供的相同值,但将加载来自yml 两个文件的值。

    还有多种方法可以定义活动配置文件。

    您可以像以前一样定义它们,在运行 jar 时使用 -Dspring.profiles.active。您还可以使用SPRING_PROFILES_ACTIVE 环境变量或spring.profiles.active 系统属性设置配置文件。

    更多信息可以在这里找到: https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-set-active-spring-profiles

    【讨论】:

    • 谢谢,你给的方式应该没问题,但正如我所说,我使用java -jar XXX.jar --spring.profiles.active=dev 选择开发配置文件,控制台打印这么多日志并警告我从未见过使用过的`java -jar XXX。 jar, and tell me APPLICATION 未能启动`但构建成功。 mvn spring-boot:run -Drun.profiles=devjava -jar -Dspring.profiles.active=dev XXX.jar一样,mvn spring-boot:runjava -jar 可以正常运行。我不小心打开调试模式???
    • 顺序错误,java -jar XXX.jar --spring.profiles.active=dev 确实将配置文件设置为 dev。您需要使用java -jar -Dspring.profiles.active=dev XXX.jar
    • 我使用java -jar -Dspring.profiles.active=dev XXX.jar 控制台打印了许多日志、警告和调试,但无法启动。 java -jar XXX.jar --spring.profiles.active=dev这个命令是错误的,但是控制台也会打印很多日志,警告和调试,和使用java -jar -Dspring.profiles.active=dev XXX.jar一样。我已经尝试了文档中的快速入门,两个命令都可以选择配置文件.springboot版本是1.4.1
    • 在这种情况下,您很可能遇到与您的个人资料无关的其他问题
    • 谢谢@francis,你拯救了我的一天
    【解决方案2】:

    如果您使用的是 Spring Boot Maven 插件,请运行:

    mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar
    

    (https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html)

    【讨论】:

      【解决方案3】:

      自 Spring Boot v2+

      我已经使用 Spring Boot v2.3.5.RELEASE 进行了验证

      使用 Spring Boot Maven 插件

      您可以像这样提供commandline argument

      mvn spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=dev"

      您可以像这样提供JVM argument

      mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev"

      java -jar

      java -Dspring.profiles.active=dev -jar app.jar (虚拟机参数)

      java -jar app.jar --spring.profiles.active=dev(程序参数)

      2021 年 4 月首次更新

      我时不时地回到这个问题上。似乎这里发生了 Windows vs *nix 的事情。

      recommendation 用于使用mvn spring-boot:run -Dspring-boot.run.profiles=dev,由于某种原因,这不适用于 Windows 7 终端(命令行提示符),至少对我来说不是。我还没有测试过 Windows 10 终端。但是它确实适用于 Babun(已停产),但不适用于 Git-Bash。尚未测试 Babun 和 Git-Bash 都基于其构建的 Cygwin。 Windows 10 和 Linux 上的 WSL 应该都能正常工作。

      2021 年 4 月第二次更新

      今天我升级到使用 Spring Boot v2.4.5 后,问题似乎消失了。
      mvn spring-boot:run -Dspring-boot.run.profiles=dev 现在可以正常工作了。

      【讨论】:

      • ./mvnw spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=prod" ,这行得通,非常感谢!
      【解决方案4】:

      您不需要三个 .yml 文件。您可以拥有一个 application.yml 文件并在其中写入配置文件特定属性,其中每个配置文件部分由 3 个连字符 (---) 分隔

      接下来,要选择当前的活动配置文件,您也可以在 application.yml 文件中指定,如下所示:

      spring:
        profiles:
          active:
          - local
      

      但是,如果您设置环境变量,则此配置将被覆盖,例如: SPRING_PROFILES_ACTIVE = 开发


      这是一个满足您要求的示例文件:

      # include common properties for every profile in this section
      
      server.port: 5000 
      
      spring:
        profiles:
          active:
          - local
      
      ---
      # profile specific properties
      
      spring:
        profiles: local
      
        datasource:
          url: jdbc:mysql://localhost:3306/
          username: root
          password: root
      
      ---
      # profile specific properties
      
      spring:
        profiles: dev
      
        datasource:
          url: jdbc:mysql://<dev db url>
          username: <username>
          password: <password>
      

      【讨论】:

        【解决方案5】:

        如果您使用的是 maven,请在 pom.xml 中定义您的配置文件,如下所示

        <profiles>
        <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <jdbc.url>dbUrl</jdbc.url>
                <jdbc.username>dbuser</jdbc.username>
                <jdbc.password>dbPassword</jdbc.password>
                <jdbc.driver>dbDriver</jdbc.driver>
            </properties>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <jdbc.url>dbUrl</jdbc.url>
                <jdbc.username>dbuser</jdbc.username>
                <jdbc.password>dbPassword</jdbc.password>
                <jdbc.driver>dbDriver</jdbc.driver>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>org.postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                </dependency>
            </dependencies>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <jdbc.url>dbUrl</jdbc.url>
                <jdbc.username>dbuser</jdbc.username>
                <jdbc.password>dbPassword</jdbc.password>
                <jdbc.driver>dbDriver</jdbc.driver>
            </properties>
        </profile>
        

        默认情况下,即如果未选择任何配置文件,则将始终使用本地配置文件。

        要在 Spring Boot 2.x.x 中选择特定配置文件,请使用以下命令。

        mvn spring-boot:run -Dspring-boot.run.profiles=dev
        

        如果您想使用特定配置文件的属性构建/编译,请使用以下命令。

        mvn clean install -Pdev -DprofileIdEnabled=true
        

        【讨论】:

        • 这仍然应该适用于任意属性吗?我正在查看我生成的战争文件,它仍然具有原始属性。
        【解决方案6】:

        mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar

        **来源-**https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html

        基本上,当您的项目中存在多个 application-{environment}.properties 时需要它。默认情况下,如果您在命令行中传递 -Drun.profiles 或在

        中传递 activeByDefault true
         <profile>
            <id>dev</id>
            <properties>
                <activatedProperties>dev</activatedProperties>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        

        没有像上面定义的那样默认选择 application.properties 否则你需要通过附加 -Drun.profiles={dev/stage/prod} 来选择。

        TL;DR

        mvn spring-boot:run -Drun.profiles=dev

        【讨论】:

          【解决方案7】:

          如果你使用 maven,

          <build>
                  <plugins>
                      <plugin>
                          <groupId>org.springframework.boot</groupId>
                          <artifactId>spring-boot-maven-plugin</artifactId>
                          <configuration>
                              <profiles>
                                  <profile>dev</profile>
                              </profiles>
                          </configuration>
                      </plugin>
                  </plugins>
              </build>
          

          这将 dev 设置为活动配置文件

          ./mvnw spring-boot:run
          

          将 dev 作为活动配置文件。

          【讨论】:

            【解决方案8】:

            您可以根据配置文件在一个 application.properties(yml) 中指定属性,例如here。然后 mvn clean spring-boot:run -Dspring.profiles.active=dev 应该正确运行它。它对我有用

            【讨论】:

            • -Dspring.profiles.active=dev 是否​​等同于 -Dspring-boot.run.profiles=dev?这令人沮丧...... brrrrr。
            • @SimonLogic,请参阅 Amimo Benja 的回答。
            • 这个答案对我不起作用。相反,请参阅@francis 的答案,mvn spring-boot:run -Dspring-boot.run.profiles=dev 有效。
            • 我正在使用Spring Boot Maven Plugin,所以我这样做:mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar(如下面的其他答案所述)。
            【解决方案9】:

            在资源目录中为运行应用程序所需的每个环境(例如:dev、qa、stg 等)创建特定的 .yml 文件。 image of .yml files in resources directory

            如果您在 pom.xml 文件中使用 spring-boot-maven-plugin 2.0.5.RELEASE,您可以在依赖标签中添加配置文件,如下所示。 image of pom.xml spring-boot-maven-plugin (您可以使用多个配置文件标签配置多个配置文件)

            然后就可以使用下面的命令来构建和运行项目了。

            1) mvn clean install
            2) mvn spring-boot:run -Dspring-boot.run.default==qa
            

            然后您会看到在运行项目时默认配置文件设置为 qa。 displaying the default profile when running the application

            【讨论】:

              【解决方案10】:

              使用 Intellij,因为我不知道如何将键盘快捷键设置为mvn spring-boot:run -Dspring.profiles.active=dev,所以我必须这样做:

              <plugin>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-maven-plugin</artifactId>
                  <configuration>
                      <jvmArguments>
                          -Dspring.profiles.active=dev
                      </jvmArguments>
                  </configuration>
              </plugin>
              

              【讨论】:

                【解决方案11】:

                @Profile 注解允许你表明一个组件是 当一个或多个指定的配置文件符合注册条件时 积极的。使用上面的示例,我们可以重写 dataSource 配置如下:

                @Configuration
                @Profile("dev")
                public class StandaloneDataConfig {
                
                    @Bean
                    public DataSource dataSource() {
                        return new EmbeddedDatabaseBuilder()
                            .setType(EmbeddedDatabaseType.HSQL)
                            .addScript("classpath:com/bank/config/sql/schema.sql")
                            .addScript("classpath:com/bank/config/sql/test-data.sql")
                            .build();
                    }
                }
                

                还有一个:

                @Configuration
                @Profile("production")
                public class JndiDataConfig {
                
                    @Bean(destroyMethod="")
                    public DataSource dataSource() throws Exception {
                        Context ctx = new InitialContext();
                        return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
                    }
                }
                

                【讨论】:

                  【解决方案12】:

                  在 Intellij IDEA 中使用“-Dspring-boot.run.profiles=foo,local”。它正在工作。它设置了 2 个配置文件“foo 和 local”。

                  已通过启动版本“2.3.2.RELEASE”和 Intellij IDEA CE 2019.3 验证。

                  <build>
                          <plugins>
                              <plugin>
                                  <groupId>org.springframework.boot</groupId>
                                  <artifactId>spring-boot-maven-plugin</artifactId>
                              </plugin>
                          </plugins>
                      </build>
                  
                  

                  使用“mvn spring-boot:run”设置配置文件

                  设置环境变量

                  【讨论】:

                    【解决方案13】:

                    或者,可以通过添加以下行直接在 application.properties 文件中指定配置文件:

                    spring.profiles.active=prod

                    配置文件与 Spring Boot 属性文件一起工作。默认情况下,Spring Boot 会解析一个名为 application.properties 的文件——位于 src/main/resources 目录中——以识别配置信息。

                    我们的第一个任务是在该文件中添加一个参数,该参数将告诉 Spring 使用与活动配置文件(即应用当前正在运行的配置文件)对应的不同环境特定属性文件。我们可以通过将以下内容添加到 application.properties 文件来做到这一点:

                    spring.profiles.active=@activatedProperties@

                    现在我们需要创建两个新的特定于环境的属性文件(在与现有 application.properties 文件相同的路径中),一个由 DEV 配置文件使用,一个由 PROD 配置文件使用。这些文件需要命名如下:

                    应用程序-dev.properties

                    应用程序-prod.properties

                    在每种情况下,我们都将 prod 指定为活动配置文件,这会导致选择 application-prod.properties 文件进行配置。

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2019-03-08
                      • 1970-01-01
                      • 2015-11-11
                      • 2016-08-19
                      • 1970-01-01
                      • 2020-05-07
                      • 2018-11-29
                      相关资源
                      最近更新 更多