第一種:
現在我把資源文件的路徑放在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 指定文件地址 多個文件用 ","隔開