网上的资料一般在使用@PropertySource的时候,都喜欢用@PropertySource("classpath:/document.properties")
这种形式
现在我把资源文件的路径放在application.properties里
config.path=/home/myservice/config.properties
然后在java源码里:
@PropertySource(value = {"${config.path}"}, encoding="utf-8")
public class MyConfig {
@Value("${myconfig.index}")
private String index;
public String getIndex() {
return index;
}
}
但是出错:
Caused by: java.io.FileNotFoundException: class path resource [home/myservice/config.properties] cannot be opened because it does not exist
你是直接读取绝对路径的磁盘文件,需要加上 file:${config:path}