Java 如何使用 Spring Boot 配置文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40060989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:40:52  来源:igfitidea点击:

how to use Spring Boot profiles

javaspringspring-boot

提问by twogoods

i have application.yml,application-dev.ymlandapplication-dev.yml

我有application.ymlapplication-dev.yml并且application-dev.yml

  1. I'm using the maven command mvn spring-boot:run -Dspring.profiles.active=devit doesn't work and I can not choose the dev profile using mvn spring-boot:run. How do I choose it?
  2. The documentation says java -jar XXX.jar --spring.profiles.active=devworks, and I tried -Dspring.profiles.active=devbut it does not work. And in my project, i use java -jar XXX.jarit runs, but if I use java -jar XXX.jar --spring.profiles.active=devto choose dev profile, console print so many logs and warns that i never see used java -jar XXX.jar,and tell me APPLICATION FAILED TO START
  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

so how to solve two problems? thanks~

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

回答by Sergii Getman

You can specify properties according profiles in one application.properties(yml)like here. Then mvn clean spring-boot:run -Dspring.profiles.active=devshould run it correct. It works for me

您可以根据指定的一个配置文件的属性application.properties(阳明)喜欢这里。然后 mvn clean spring-boot:run -Dspring.profiles.active=dev应该正确运行它。这个对我有用

回答by francis

I'm not sure I fully understand the question but I'll attempt to answer by providing a few details about profiles in Spring Boot.

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

For your #1 example, according to the docs you can select the profile using the Spring Boot Maven plugin using -Drun.profiles.

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

Edit: For Spring Boot 2.0+ runhas been renamed to spring-boot.run

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

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

http://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html

http://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html

From your #2 example, you are defining the active profile after the name of the jar. You need to provide the JVM argument before the name of the jar you are running.

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

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

General info:

基本信息:

You mention that you have both an application.ymland a application-dev.yml. Running with the devprofile will actually load bothconfig files. Values from application-dev.ymlwill override the same values provided by application.ymlbut values from both ymlfiles will be loaded.

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

There are also multiple ways to define the active profile.

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

You can define them as you did, using -Dspring.profiles.activewhen running your jar. You can also set the profile using a SPRING_PROFILES_ACTIVEenvironment variable or a spring.profiles.activesystem property.

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

More info can be found here: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-set-active-spring-profiles

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

回答by sumit singh

You don't need three .yml files for this. You can have a single application.yml file and write profile specific properties in the same where each profile section is separated by 3 hyphen (---)

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

Next, for selecting the current active profile, you can specify that as well in your application.yml file, like this :

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

spring:
  profiles:
    active:
    - local

However, this configuration will be overriden if you set an Environment variable, eg : SPRING_PROFILES_ACTIVE = dev

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



Here is a sample file for you requirement:

这是您要求的示例文件:

# 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>

回答by Pedro H

If you are using the Spring Boot Maven Plugin, run:

如果您使用的是 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)

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

回答by Adam Ostro?lík

The @Profile annotation allows you to indicate that a component is eligible for registration when one or more specified profiles are active. Using our example above, we can rewrite the dataSource configuration as follows:

@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();
    }
}

And other one:

还有一个:

@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");
    }
}

回答by Ishadi Gamage

Create specific .yml files in the resources directory for each and every environment(Eg: dev,qa,stg etc.) that you need to run the application. image of .yml files in resources directory

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

If you are using spring-boot-maven-plugin 2.0.5.RELEASE in your pom.xml file you can add the profiles within the dependency tag as follows. image of pom.xml spring-boot-maven-plugin(you can configure multiple profiles using multiple profile tags)

如果您在 pom.xml 文件中使用 spring-boot-maven-plugin 2.0.5.RELEASE,则可以按如下方式在依赖项标记中添加配置文件。 pom.xml spring-boot-maven-plugin 的镜像(您可以使用多个配置文件标签配置多个配置文件)

Then you can use the following commands to build and run the project.

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

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

Then you will see that the default profile is set as qa while running the project. displaying the default profile when running the application

然后您将看到在运行项目时默认配置文件设置为 qa。 运行应用程序时显示默认配置文件

回答by Amimo Benja

If you are using maven, define your profiles as shown below within your pom.xml

如果您使用的是 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>

By default, i.e if No profile is selected, the local profile will always be use.

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

To select a specific profile in Spring Boot 2.x.x, use the below command.

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

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

If you want want to build/compile using properties of a specific profile, use the below command.

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

mvn clean install -Pdev -DprofileIdEnabled=true

回答by kris

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

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

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

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

Basically it is need when you multiple application-{environment}.properties is present inside in your project. by default, if you passed -Drun.profiles on command line or activeByDefault true in

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

 <profile>
    <id>dev</id>
    <properties>
        <activatedProperties>dev</activatedProperties>
    </properties>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
</profile>

Nothing defined like above it will choose by default application.properties otherwise you need to select by appending -Drun.profiles={dev/stage/prod}.

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

TL;DR

TL; 博士

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

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

回答by Sida Zhou

Working with Intellij, because I don't know how to set keyboard shortcut to mvn spring-boot:run -Dspring.profiles.active=dev, I have to do this:

使用 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>