关键类:
org.springframework.boot.context.config.ConfigFileApplicationListener
最终加载文件的方法在其内部类Loader的load方法:
private void load(PropertySourceLoader loader, String location, Profile profile,DocumentFilter filter, DocumentConsumer consumer)
下面是我改写的方法,大家把这个方法替换掉,运行一遍即可知道springboot会去加载哪些配置文件
private void load(PropertySourceLoader loader, String location, Profile profile,
DocumentFilter filter, DocumentConsumer consumer) {
try {
System.out.println("开始尝试加载配置文件:"+location+ ":::"+resourceLoader.getClass().getName());
Resource resource = this.resourceLoader.getResource(location);
if(resource instanceof org.springframework.core.io.FileUrlResource) {
File file = resource.getFile();
ResourceUtils.getFile(resource.getURL(), resource.getDescription());
}
if (resource == null || !resource.exists()) {
try {
System.out.println("不存在的配置文件"+resource.getFile().getAbsolutePath());
}catch(Exception e) {
System.out.println(e.getMessage());
}
if (this.logger.isTraceEnabled()) {
StringBuilder description = getDescription(
"Skipped missing config ", location, resource, profile);
this.logger.trace(description);
}
return;
}
if (!StringUtils.hasText(
StringUtils.getFilenameExtension(resource.getFilename()))) {
if (this.logger.isTraceEnabled()) {
StringBuilder description = getDescription(
"Skipped empty config extension ", location, resource,
profile);
this.logger.trace(description);
}
return;
}
String name = "applicationConfig: [" + location + "]";
List<Document> documents = loadDocuments(loader, name, resource);
if (CollectionUtils.isEmpty(documents)) {
try {
System.out.println("配置文件内容为空"+resource.getFile().getAbsolutePath());
}catch(Exception e) {
System.out.println(e.getMessage());
}
if (this.logger.isTraceEnabled()) {
StringBuilder description = getDescription(
"Skipped unloaded config ", location, resource, profile);
this.logger.trace(description);
}
return;
}
List<Document> loaded = new ArrayList<>();
for (Document document : documents) {
if (filter.match(document)) {
addActiveProfiles(document.getActiveProfiles());
addIncludedProfiles(document.getIncludeProfiles());
loaded.add(document);
}
}
Collections.reverse(loaded);
if (!loaded.isEmpty()) {
loaded.forEach((document) -> consumer.accept(profile, document));
Resource resource1 = this.resourceLoader.getResource(location);
try {
System.out.println("######################成功加载文件"+resource.getFile().getAbsolutePath()+":::"+resource1.getClass().getName());
}catch(Exception e) {
System.out.println(e.getMessage());
}
if (this.logger.isDebugEnabled()) {
StringBuilder description = getDescription("Loaded config file ",
location, resource, profile);
this.logger.debug(description);
}
}
}
catch (Exception ex) {
System.out.println("加载配置文件异常");
throw new IllegalStateException("Failed to load property "
+ "source from location '" + location + "'", ex);
}finally {
System.out.println("加载完毕-----------------------------");
}
}