调试源代码,配置文件加载代码位置是:
org.springframework.boot.context.config.ConfigFileApplicationListener
public void postProcessEnvironment(ConfigurableEnvironment environment,SpringApplication application)方法
这个方法执行完,enviroment->propertySources从4个,变成6个,最终加载完成
先读取property,再读取xml,最后是yml文件
读取文件的代码在方法
private void load(String location, String name, Profile profile,DocumentFilterFactory filterFactory, DocumentConsumer consumer)中
462行(spring-boot-2.0.1-RELEASE版)
500行 Resource resource = this.resourceLoader.getResource(location);打断点调试,看能否读取配置文件正确路径
如果配置文件在resources下,location变量应该为classpath:/application.yml
在IDE环境错误的情况下,Spring找不到这个文件!!!
可以在调试中通过this.resourceLoader.getResource("classpath:/application.yml").getURI()进行测试,找不到就是IDE有问题或工程有问题。
这种问题比较麻烦,谁知道ide到底出了什么问题?一般工程设置了resources会自动将resources加入classpath,如果实在没有手动将resources加入classpath就完成了,唯一的问题是用vcs同步开发的时候比较痛苦。
本文详细解析了Spring Boot中配置文件加载的过程,包括如何通过ConfigFileApplicationListener调整配置环境,以及如何解决IDE环境下配置文件加载失败的问题。
1万+

被折叠的 条评论
为什么被折叠?



