Polish PropertySourceProcessor

This commit is contained in:
Sam Brannen
2023-08-04 16:07:22 +03:00
parent 4b54ca46d3
commit 1451f30781

View File

@@ -49,22 +49,24 @@ import org.springframework.util.ReflectionUtils;
*/
public class PropertySourceProcessor {
private static final PropertySourceFactory DEFAULT_PROPERTY_SOURCE_FACTORY = new DefaultPropertySourceFactory();
private static final PropertySourceFactory defaultPropertySourceFactory = new DefaultPropertySourceFactory();
private static final Log logger = LogFactory.getLog(PropertySourceProcessor.class);
private final ConfigurableEnvironment environment;
private final ResourceLoader resourceLoader;
private final List<String> propertySourceNames;
private final List<String> propertySourceNames = new ArrayList<>();
public PropertySourceProcessor(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
this.environment = environment;
this.resourceLoader = resourceLoader;
this.propertySourceNames = new ArrayList<>();
}
/**
* Process the specified {@link PropertySourceDescriptor} against the
* environment managed by this instance.
@@ -78,7 +80,7 @@ public class PropertySourceProcessor {
Assert.isTrue(locations.size() > 0, "At least one @PropertySource(value) location is required");
boolean ignoreResourceNotFound = descriptor.ignoreResourceNotFound();
PropertySourceFactory factory = (descriptor.propertySourceFactory() != null ?
instantiateClass(descriptor.propertySourceFactory()) : DEFAULT_PROPERTY_SOURCE_FACTORY);
instantiateClass(descriptor.propertySourceFactory()) : defaultPropertySourceFactory);
for (String location : locations) {
try {
@@ -100,13 +102,13 @@ public class PropertySourceProcessor {
}
}
private void addPropertySource(org.springframework.core.env.PropertySource<?> propertySource) {
private void addPropertySource(PropertySource<?> propertySource) {
String name = propertySource.getName();
MutablePropertySources propertySources = this.environment.getPropertySources();
if (this.propertySourceNames.contains(name)) {
// We've already added a version, we need to extend it
org.springframework.core.env.PropertySource<?> existing = propertySources.get(name);
PropertySource<?> existing = propertySources.get(name);
if (existing != null) {
PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource rps ?
rps.withResourceName() : propertySource);
@@ -136,7 +138,8 @@ public class PropertySourceProcessor {
this.propertySourceNames.add(name);
}
private PropertySourceFactory instantiateClass(Class<? extends PropertySourceFactory> type) {
private static PropertySourceFactory instantiateClass(Class<? extends PropertySourceFactory> type) {
try {
Constructor<? extends PropertySourceFactory> constructor = type.getDeclaredConstructor();
ReflectionUtils.makeAccessible(constructor);