代码优化

This commit is contained in:
卢春梦 2024-09-26 13:50:33 +08:00
parent 8c72a12d9a
commit 443ee7188d

View File

@ -39,7 +39,6 @@ import org.springframework.util.StringUtils;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
* 自定义资源文件读取优先级最低 * 自定义资源文件读取优先级最低
@ -92,13 +91,13 @@ public class BladePropertySourcePostProcessor implements BeanFactoryPostProcesso
List<PropertyFile> sortedPropertyList = propertyFileList.stream() List<PropertyFile> sortedPropertyList = propertyFileList.stream()
.distinct() .distinct()
.sorted() .sorted()
.collect(Collectors.toList()); .toList();
ConfigurableEnvironment environment = beanFactory.getBean(ConfigurableEnvironment.class); ConfigurableEnvironment environment = beanFactory.getBean(ConfigurableEnvironment.class);
MutablePropertySources propertySources = environment.getPropertySources(); MutablePropertySources propertySources = environment.getPropertySources();
// 只支持 activeProfiles没有必要支持 spring.profiles.include // 只支持 activeProfiles没有必要支持 spring.profiles.include
String[] activeProfiles = environment.getActiveProfiles(); String[] activeProfiles = environment.getActiveProfiles();
ArrayList<PropertySource> propertySourceList = new ArrayList<>(); List<PropertySource> propertySourceList = new ArrayList<>();
for (String profile : activeProfiles) { for (String profile : activeProfiles) {
for (PropertyFile propertyFile : sortedPropertyList) { for (PropertyFile propertyFile : sortedPropertyList) {
// 不加载 ActiveProfile 的配置文件 // 不加载 ActiveProfile 的配置文件
@ -112,7 +111,7 @@ public class BladePropertySourcePostProcessor implements BeanFactoryPostProcesso
} }
String location = propertyFile.getLocation(); String location = propertyFile.getLocation();
String filePath = StringUtils.stripFilenameExtension(location); String filePath = StringUtils.stripFilenameExtension(location);
String profiledLocation = filePath + "-" + profile + "." + extension; String profiledLocation = filePath + '-' + profile + '.' + extension;
Resource resource = resourceLoader.getResource(profiledLocation); Resource resource = resourceLoader.getResource(profiledLocation);
loadPropertySource(profiledLocation, resource, loader, propertySourceList); loadPropertySource(profiledLocation, resource, loader, propertySourceList);
} }
@ -135,7 +134,7 @@ public class BladePropertySourcePostProcessor implements BeanFactoryPostProcesso
PropertySourceLoader loader, PropertySourceLoader loader,
List<PropertySource> sourceList) { List<PropertySource> sourceList) {
if (resource.exists()) { if (resource.exists()) {
String name = "bladePropertySource: [" + location + "]"; String name = "bladePropertySource: [" + location + ']';
try { try {
sourceList.addAll(loader.load(name, resource)); sourceList.addAll(loader.load(name, resource));
} catch (IOException e) { } catch (IOException e) {