简体   繁体   English

spring boot中application.yml是按什么顺序读取的?

[英]In what order are application.yml read in spring boot?

I suppose that it reads in work working circuit and then it reads in testing circuit .我想它在工作工作电路中读取,然后在测试电路中读取。

If application.yml is in testing circuit , it settings replace the settings has been reading in application.yml of working circuit.如果application.yml测试电路中,它的设置会替换工作电路的application.yml中已经读取的设置 If You have defined application-test.yml and you point above a class an annotation @SpringBootTest and above @ActiveProfile("test") then the settings from application-test.yml replaces earlier settings.如果您已经定义了application-test.yml并且您指向一个类上方的注释@SpringBootTest和上方的@ActiveProfile("test")那么application-test.yml 中的设置将替换之前的设置。

what do you think about all this ?你怎么看这一切? How to understand this process correctly?如何正确理解这个过程?

  • Terminology:术语:

Work Circuit : /src/main/resources工作电路:/src/main/resources

Test Circuit : /src/test/resources测试电路:/src/test/resources

  • Work Circut工作流程

application.yaml应用程序.yaml

main:
  car:
    name: "Audi"
    id: "10201"

application-local.yaml应用程序-local.yaml

main:
  car:
    name: "Mercedes"
    id: "10200"
  • main class主班
@SpringBootApplication
public class SpringProfilesApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringProfilesApplication.class, args);
    }

    @Bean
    CommandLineRunner commandLineRunner(ProfileMain profileMain) {
        return args -> {
            profileMain.printMainName();

        };
    }
}
  • class班级
@Component
public class ProfileMain {

    @Value("${main.car.name}")
    private String name;

    @Value("${main.car.id}")
    private String id;

    public void printMainName() {
        System.out.println("\n name in Main application.yaml : " + this.name + "\n");
        System.out.println("\n name in Main application.yaml : " + this.id + "\n");
    }
}

– When you run the main class, without specifying any profiles, application.yaml will be used. – 运行主类时,不指定任何配置文件,将使用application.yaml – You can specify the profile you need in the development environment – 可以在开发环境中指定自己需要的profile

在此处输入图片说明

Now the settings of those properties that were in the main application.yaml , will be overridden in the specified profile - local .现在,主application.yaml 中的那些属性的设置将在指定的 profile- local 中被覆盖。 If the selected profile does not contain the settings that are in the main application.yaml , then the missing properties will be taken from the main application.yaml .如果所选配置文件不包含主application.yaml 中的设置,则将从主application.yaml 中获取缺少的属性。

According to Spring Boot reference documentation , the configuration data files are considered in the following order:根据Spring Boot 参考文档,配置数据文件按以下顺序考虑:

  1. Application properties packaged inside your jar (application.properties and YAML variants).打包在 jar 中的应用程序属性(application.properties 和 YAML 变体)。
  2. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).打包在 jar 中的特定于配置文件的应用程序属性(application-{profile}.properties 和 YAML 变体)。
  3. Application properties outside of your packaged jar (application.properties and YAML variants).打包 jar 之外的应用程序属性(application.properties 和 YAML 变体)。
  4. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).打包 jar 之外的特定于配置文件的应用程序属性(application-{profile}.properties 和 YAML 变体)。

you have to put this in your application-test.yml你必须把它放在你的application-test.yml 中

spring:
  profiles: test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Spring Boot中从application.yml文件读取编号 - Read number from application.yml file in spring boot spring 启动应用程序.yml 中的环境变量 - Environment variable in spring boot application.yml Spring Boot application.yml 和 @Value 不起作用 - Spring Boot application.yml and @Value is not working 在 spring boot 中将属性放在 application.yml 或 bootstrap.yml 上有什么区别? - What is the difference between putting a property on application.yml or bootstrap.yml in spring boot? 如何从 spring-boot 应用程序中的 application.yml 文件中读取属性 - How to read properties from application.yml file in spring-boot application Docker 图像无法读取 Spring 引导应用程序上的 application.yml 文件 - Docker image can't read application.yml file on Spring Boot Application Spring Boot-无法读取Yaml属性文件application.yml - spring boot- unable to read yaml properties file application.yml 如何使用Spring Boot从application.yml读取变量并在Kotlin测试中使用它 - How to read variable from application.yml with spring boot and use it in kotlin test Flyway-maven-plugin如何从Spring Boot application.yml中读取设置 - Flyway-maven-plugin how to read the settings from spring boot application.yml Spring Boot - 无法从实例化对象中读取 application.yml 值 - Spring Boot - Cannot read application.yml values from instantiated object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM