首页 > 其他 > 详细

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

时间:2020-05-06 22:10:13      阅读:299      评论:0      收藏:0      [点我收藏+]
 

第一种:

现在我把资源文件的路径放在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 指定文件地址 多个文件用 ","隔开

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

原文:https://www.cnblogs.com/kelelipeng/p/12837825.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!