Revised @PropertySource parsing for consistent PropertySource naming, avoiding accidental overriding by name

Issue: SPR-11637
(cherry picked from commit ab24dda)
This commit is contained in:
Juergen Hoeller
2014-04-28 23:24:57 +02:00
parent 89398b0811
commit bd5b3749c5
4 changed files with 74 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,13 +30,18 @@ import java.util.Properties;
* {@link Properties#getProperty} and {@link Properties#setProperty}.
*
* @author Chris Beams
* @author Juergen Hoeller
* @since 3.1
*/
public class PropertiesPropertySource extends MapPropertySource {
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({"unchecked", "rawtypes"})
public PropertiesPropertySource(String name, Properties source) {
super(name, (Map) source);
}
protected PropertiesPropertySource(String name, Map<String, Object> source) {
super(name, source);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.core.io.support;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import org.springframework.core.env.PropertiesPropertySource;
@@ -36,6 +37,8 @@ import org.springframework.util.StringUtils;
* @author Chris Beams
* @author Juergen Hoeller
* @since 3.1
* @see org.springframework.core.io.Resource
* @see org.springframework.core.io.support.EncodedResource
*/
public class ResourcePropertySource extends PropertiesPropertySource {
@@ -112,10 +115,27 @@ public class ResourcePropertySource extends PropertiesPropertySource {
this(new DefaultResourceLoader().getResource(location));
}
private ResourcePropertySource(String name, Map<String, Object> source) {
super(name, source);
}
/**
* Return the description string for the resource, and if empty returns
* the class name of the resource plus its identity hash code.
* Return a potentially adapted variant of this {@link ResourcePropertySource},
* overriding the previously given (or derived) name with the specified name.
*/
public ResourcePropertySource withName(String name) {
if (this.name.equals(name)) {
return this;
}
return new ResourcePropertySource(name, this.source);
}
/**
* Return the description String for the given Resource; it the description is
* empty, return the class name of the resource plus its identity hash code.
* @see org.springframework.core.io.Resource#getDescription()
*/
private static String getNameForResource(Resource resource) {
String name = resource.getDescription();