@PropertySource加载文件的两种用法以及配置文件加载顺序

 

 

第一种:

现在我把资源文件的路径放在application.properties里

config.path=/home/myservice/config.properties

@PropertySource(value = {"file:${config.path}"}, encoding="utf-8")
public class MyConfig {
    @Value("${myconfig.index}")
    private String index;

    public String getIndex() {
       return index;
    }
}
配置文件中 配置文件是绝对路径时 用:file

第二种
@PropertySource("classpath:/document.properties")

public class MyConfig {
    @Value("${myconfig.index}")
    private String index;

    public String getIndex() {
       return index;
    }
}
classpath路径下就是可以用第二种方式

springboot 文件加载顺序 一般默认加载application.propertise 或者 application.yml文件
jar包目录下
1.config
2.jar同级目录
class目录下
3.config目录
4.class同级目录
相同属性 按照优先级最高的匹配

另外:可以在启动命令中 加入 --spring.config.location 指定文件地址 多个文件用 ","隔开
原文地址:https://www.cnblogs.com/kelelipeng/p/12837825.html