Revised ResourcePropertySource in order to avoid ConfigurationClassProcessor's AnnotationPropertySource subclass
Issue: SPR-12115
This commit is contained in:
@@ -55,15 +55,24 @@ public class CompositePropertySource extends PropertySource<Object> {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given {@link PropertySource} to the end of the chain.
|
||||
* @param propertySource the PropertySource to add
|
||||
*/
|
||||
public void addPropertySource(PropertySource<?> propertySource) {
|
||||
this.propertySources.add(propertySource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given {@link PropertySource} to the start of the chain.
|
||||
* @param propertySource the PropertySource to add
|
||||
* @since 4.1
|
||||
*/
|
||||
public void addFirstPropertySource(PropertySource<?> propertySource) {
|
||||
List<PropertySource<?>> exisiting = new ArrayList<PropertySource<?>>(this.propertySources);
|
||||
List<PropertySource<?>> existing = new ArrayList<PropertySource<?>>(this.propertySources);
|
||||
this.propertySources.clear();
|
||||
this.propertySources.add(propertySource);
|
||||
this.propertySources.addAll(exisiting);
|
||||
this.propertySources.addAll(existing);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -42,12 +42,17 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class ResourcePropertySource extends PropertiesPropertySource {
|
||||
|
||||
/** The original resource name, if different from the given name */
|
||||
private final String resourceName;
|
||||
|
||||
|
||||
/**
|
||||
* Create a PropertySource having the given name based on Properties
|
||||
* loaded from the given encoded resource.
|
||||
*/
|
||||
public ResourcePropertySource(String name, EncodedResource resource) throws IOException {
|
||||
super(name, PropertiesLoaderUtils.loadProperties(resource));
|
||||
this.resourceName = getNameForResource(resource.getResource());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +61,8 @@ public class ResourcePropertySource extends PropertiesPropertySource {
|
||||
* {@link Resource#getDescription() description} of the given resource.
|
||||
*/
|
||||
public ResourcePropertySource(EncodedResource resource) throws IOException {
|
||||
this(getNameForResource(resource.getResource()), resource);
|
||||
super(getNameForResource(resource.getResource()), PropertiesLoaderUtils.loadProperties(resource));
|
||||
this.resourceName = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,6 +71,7 @@ public class ResourcePropertySource extends PropertiesPropertySource {
|
||||
*/
|
||||
public ResourcePropertySource(String name, Resource resource) throws IOException {
|
||||
super(name, PropertiesLoaderUtils.loadProperties(new EncodedResource(resource)));
|
||||
this.resourceName = getNameForResource(resource);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,7 +80,8 @@ public class ResourcePropertySource extends PropertiesPropertySource {
|
||||
* {@link Resource#getDescription() description} of the given resource.
|
||||
*/
|
||||
public ResourcePropertySource(Resource resource) throws IOException {
|
||||
this(getNameForResource(resource), resource);
|
||||
super(getNameForResource(resource), PropertiesLoaderUtils.loadProperties(new EncodedResource(resource)));
|
||||
this.resourceName = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -115,20 +123,47 @@ public class ResourcePropertySource extends PropertiesPropertySource {
|
||||
this(new DefaultResourceLoader().getResource(location));
|
||||
}
|
||||
|
||||
protected ResourcePropertySource(String name, Map<String, Object> source) {
|
||||
private ResourcePropertySource(String name, String resourceName, Map<String, Object> source) {
|
||||
super(name, source);
|
||||
this.resourceName = resourceName;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return a potentially adapted variant of this {@link ResourcePropertySource},
|
||||
* overriding the previously given (or derived) name with the specified name.
|
||||
* @since 4.0.4
|
||||
*/
|
||||
public ResourcePropertySource withName(String name) {
|
||||
if (this.name.equals(name)) {
|
||||
return this;
|
||||
}
|
||||
return new ResourcePropertySource(name, this.source);
|
||||
// Store the original resource name if necessary...
|
||||
if (this.resourceName != null) {
|
||||
if (this.resourceName.equals(name)) {
|
||||
return new ResourcePropertySource(this.resourceName, null, this.source);
|
||||
}
|
||||
else {
|
||||
return new ResourcePropertySource(name, this.resourceName, this.source);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Current name is resource name -> preserve it in the extra field...
|
||||
return new ResourcePropertySource(name, this.name, this.source);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a potentially adapted variant of this {@link ResourcePropertySource},
|
||||
* overriding the previously given name (if any) with the original resource name
|
||||
* (equivalent to the name generated by the name-less constructor variants).
|
||||
* @since 4.1
|
||||
*/
|
||||
public ResourcePropertySource withResourceName() {
|
||||
if (this.resourceName == null) {
|
||||
return this;
|
||||
}
|
||||
return new ResourcePropertySource(this.resourceName, null, this.source);
|
||||
}
|
||||
|
||||
|
||||
@@ -137,7 +172,7 @@ public class ResourcePropertySource extends PropertiesPropertySource {
|
||||
* empty, return the class name of the resource plus its identity hash code.
|
||||
* @see org.springframework.core.io.Resource#getDescription()
|
||||
*/
|
||||
protected static String getNameForResource(Resource resource) {
|
||||
private static String getNameForResource(Resource resource) {
|
||||
String name = resource.getDescription();
|
||||
if (!StringUtils.hasText(name)) {
|
||||
name = resource.getClass().getSimpleName() + "@" + System.identityHashCode(resource);
|
||||
|
||||
Reference in New Issue
Block a user