5、SpringBoot的Profile功能
private static final String DEFAULT_NAMES = "application";// Set<string> names = isFolder ? getSearchNames() : NO_SEARCH_NAMES;
private Set<string> getSearchNames() {
if (this.environment.containsProperty(CONFIG_NAME_PROPERTY)) {
String property = this.environment.getProperty(CONFIG_NAME_PROPERTY);
return asResolvedSet(property, null);
}
// 返回默认的配置文件的前缀"application";
return asResolvedSet(ConfigFileApplicationListener.this.names, DEFAULT_NAMES);
}
// names.forEach((name) -> load(location, name, profile, filterFactory, consumer)); private void load(String location, String name, Profile profile, DocumentFilterFactory filterFactory,
DocumentConsumer consumer) {
// name默认是application,跳过
if (!StringUtils.hasText(name)) {
for (PropertySourceLoader loader : this.propertySourceLoaders) {
if (canLoadFileExtension(loader, location)) {
load(loader, location, profile, filterFactory.getDocumentFilter(profile), consumer);
return;
}
}
}
Set<string> processed = new HashSet<>();
// this.propertySourceLoaders见下图
for (PropertySourceLoader loader : this.propertySourceLoaders) {
// 返回每个loader可以处理的扩展名,PropertiesPropertySourceLoader可以处理{.properties,.xml},
// YamlPropertySourceLoader可以处理{.yml,.yaml}格式的文件
for (String fileExtension : loader.getFileExtensions()) {
if (processed.add(fileExtension)) {
loadForFileExtension(loader, location + name, "." + fileExtension, profile, filterFactory, consumer);
}
}
}
}
private void load(PropertySourceLoader loader, String location, Profile profile, DocumentFilter filter, DocumentConsumer consumer) {
try {
// 加载资源
Resource resource = this.resourceLoader.getResource(location);
// applicationConfig: [classpath:/application.properties]
String name = "applicationConfig: [" + location + "]";
List<document> documents = loadDocuments(loader, name, resource);
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));
}
}
}
```</document></document></string></string></string></string></string></string></profile></profile></profile></profile></profile></environmentpostprocessor>
以上是 5、SpringBoot的Profile功能 的全部内容, 来源链接: utcz.com/z/513542.html