SpringBoot - 1.加载属性文件位置与优先级

加载属性文件位置与优先级

@SpringBootApplication
public class BootConfigApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(BootConfigApplication.class, args);
        ConfigurableEnvironment environment = context.getEnvironment();
        String locationIpDir = environment.getProperty("location.ip.dir");
        String curDir = environment.getProperty("user.dir");
        System.out.println(locationIpDir + "," + curDir);
        context.close();
    }
}
规则一

file:./config/application.properties 当前项目路径config目录下
file:./application.properties] 当前项目路径下
classpath:/config/application.properties 类路径config目录下
classpath:/application.properties 类路径下

规则二

xxx.properties 和 xxx.yml 两中配置文件同时存在时,优先级 .properties >.yml ,同一目录文件里面没有.properties文件 则读取.yml 文件
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码跟踪,Debug

在这里插入图片描述在这里插入图片描述在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_18398239/article/details/89204931