Use ConversionService from Environment in PropertySourcesPlaceholderConfigurer
This commit fixes a regression in PropertySourcesPlaceholderConfigurer that was introduced in Spring Framework 6.2.7. Specifically, this commit reinstates automatic String-conversion of values from PropertySources in the Environment using the ConversionService configured in the Environment. See gh-34861 Closes gh-34936
This commit is contained in:
@@ -24,6 +24,8 @@ import org.springframework.beans.factory.BeanInitializationException;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.config.PlaceholderConfigurerSupport;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.support.DefaultConversionService;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.ConfigurablePropertyResolver;
|
||||
import org.springframework.core.env.Environment;
|
||||
@@ -243,16 +245,38 @@ public class PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerS
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Object getProperty(String name) {
|
||||
// Declare String as covariant return type, since a String is actually required.
|
||||
public String getProperty(String name) {
|
||||
for (PropertySource<?> propertySource : super.source.getPropertySources()) {
|
||||
Object candidate = propertySource.getProperty(name);
|
||||
if (candidate != null) {
|
||||
return candidate;
|
||||
return convertToString(candidate);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the supplied value to a {@link String} using the {@link ConversionService}
|
||||
* from the {@link Environment}.
|
||||
* <p>This is a modified version of
|
||||
* {@link org.springframework.core.env.AbstractPropertyResolver#convertValueIfNecessary(Object, Class)}.
|
||||
* @param value the value to convert
|
||||
* @return the converted value, or the original value if no conversion is necessary
|
||||
* @since 6.2.8
|
||||
*/
|
||||
@Nullable
|
||||
private String convertToString(Object value) {
|
||||
if (value instanceof String string) {
|
||||
return string;
|
||||
}
|
||||
ConversionService conversionService = super.source.getConversionService();
|
||||
if (conversionService == null) {
|
||||
conversionService = DefaultConversionService.getSharedInstance();
|
||||
}
|
||||
return conversionService.convert(value, String.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ConfigurableEnvironmentPropertySource {propertySources=" + super.source.getPropertySources() + "}";
|
||||
@@ -279,7 +303,8 @@ public class PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerS
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public Object getProperty(String name) {
|
||||
// Declare String as covariant return type, since a String is actually required.
|
||||
public String getProperty(String name) {
|
||||
return super.source.getProperty(name);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user