Deprecated getPropertyAsClass and refined PropertySourcesPropertyResolver's logging
Issue: SPR-14370
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -547,6 +547,7 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetType) {
|
||||
return this.propertyResolver.getPropertyAsClass(key, targetType);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -131,6 +131,15 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsProperty(String key) {
|
||||
return (getProperty(key) != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProperty(String key) {
|
||||
return getProperty(key, String.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getProperty(String key, String defaultValue) {
|
||||
@@ -144,6 +153,12 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe
|
||||
return (value != null ? value : defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetValueType) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRequiredProperty(String key) throws IllegalStateException {
|
||||
String value = getProperty(key);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -20,6 +20,7 @@ package org.springframework.core.env;
|
||||
* Interface for resolving properties against any underlying source.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.1
|
||||
* @see Environment
|
||||
* @see PropertySourcesPropertyResolver
|
||||
@@ -27,14 +28,14 @@ package org.springframework.core.env;
|
||||
public interface PropertyResolver {
|
||||
|
||||
/**
|
||||
* Return whether the given property key is available for resolution, i.e.,
|
||||
* the value for the given key is not {@code null}.
|
||||
* Return whether the given property key is available for resolution,
|
||||
* i.e. if the value for the given key is not {@code null}.
|
||||
*/
|
||||
boolean containsProperty(String key);
|
||||
|
||||
/**
|
||||
* Return the property value associated with the given key, or {@code null}
|
||||
* if the key cannot be resolved.
|
||||
* Return the property value associated with the given key,
|
||||
* or {@code null} if the key cannot be resolved.
|
||||
* @param key the property name to resolve
|
||||
* @see #getProperty(String, String)
|
||||
* @see #getProperty(String, Class)
|
||||
@@ -53,8 +54,8 @@ public interface PropertyResolver {
|
||||
String getProperty(String key, String defaultValue);
|
||||
|
||||
/**
|
||||
* Return the property value associated with the given key, or {@code null}
|
||||
* if the key cannot be resolved.
|
||||
* Return the property value associated with the given key,
|
||||
* or {@code null} if the key cannot be resolved.
|
||||
* @param key the property name to resolve
|
||||
* @param targetType the expected type of the property value
|
||||
* @see #getRequiredProperty(String, Class)
|
||||
@@ -62,8 +63,8 @@ public interface PropertyResolver {
|
||||
<T> T getProperty(String key, Class<T> targetType);
|
||||
|
||||
/**
|
||||
* Return the property value associated with the given key, or
|
||||
* {@code defaultValue} if the key cannot be resolved.
|
||||
* Return the property value associated with the given key,
|
||||
* or {@code defaultValue} if the key cannot be resolved.
|
||||
* @param key the property name to resolve
|
||||
* @param targetType the expected type of the property value
|
||||
* @param defaultValue the default value to return if no value is found
|
||||
@@ -75,10 +76,13 @@ public interface PropertyResolver {
|
||||
* Convert the property value associated with the given key to a {@code Class}
|
||||
* of type {@code T} or {@code null} if the key cannot be resolved.
|
||||
* @throws org.springframework.core.convert.ConversionException if class specified
|
||||
* by property value cannot be found or loaded or if targetType is not assignable
|
||||
* by property value cannot be found or loaded or if targetType is not assignable
|
||||
* from class specified by property value
|
||||
* @see #getProperty(String, Class)
|
||||
* @deprecated as of 4.3, in favor of {@link #getProperty} with manual conversion
|
||||
* to {@code Class} via the application's {@code ClassLoader}
|
||||
*/
|
||||
@Deprecated
|
||||
<T> Class<T> getPropertyAsClass(String key, Class<T> targetType);
|
||||
|
||||
/**
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.springframework.util.ClassUtils;
|
||||
* an underlying set of {@link PropertySources}.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.1
|
||||
* @see PropertySource
|
||||
* @see PropertySources
|
||||
@@ -71,55 +72,43 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
|
||||
}
|
||||
|
||||
protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) {
|
||||
boolean debugEnabled = logger.isDebugEnabled();
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace(String.format("getProperty(\"%s\", %s)", key, targetValueType.getSimpleName()));
|
||||
}
|
||||
if (this.propertySources != null) {
|
||||
for (PropertySource<?> propertySource : this.propertySources) {
|
||||
if (debugEnabled) {
|
||||
logger.debug(String.format("Searching for key '%s' in [%s]", key, propertySource.getName()));
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace(String.format("Searching for key '%s' in [%s]", key, propertySource.getName()));
|
||||
}
|
||||
Object value = propertySource.getProperty(key);
|
||||
if (value != null) {
|
||||
Class<?> valueType = value.getClass();
|
||||
if (resolveNestedPlaceholders && value instanceof String) {
|
||||
value = resolveNestedPlaceholders((String) value);
|
||||
}
|
||||
if (debugEnabled) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format("Found key '%s' in [%s] with type [%s] and value '%s'",
|
||||
key, propertySource.getName(), valueType.getSimpleName(), value));
|
||||
}
|
||||
if (!this.conversionService.canConvert(valueType, targetValueType)) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"Cannot convert value [%s] from source type [%s] to target type [%s]",
|
||||
value, valueType.getSimpleName(), targetValueType.getSimpleName()));
|
||||
key, propertySource.getName(), value.getClass().getSimpleName(), value));
|
||||
}
|
||||
return this.conversionService.convert(value, targetValueType);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (debugEnabled) {
|
||||
logger.debug(String.format("Could not find key '%s' in any property source. Returning [null]", key));
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format("Could not find key '%s' in any property source", key));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetValueType) {
|
||||
boolean debugEnabled = logger.isDebugEnabled();
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace(String.format("getPropertyAsClass(\"%s\", %s)", key, targetValueType.getSimpleName()));
|
||||
}
|
||||
if (this.propertySources != null) {
|
||||
for (PropertySource<?> propertySource : this.propertySources) {
|
||||
if (debugEnabled) {
|
||||
logger.debug(String.format("Searching for key '%s' in [%s]", key, propertySource.getName()));
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace(String.format("Searching for key '%s' in [%s]", key, propertySource.getName()));
|
||||
}
|
||||
Object value = propertySource.getProperty(key);
|
||||
if (value != null) {
|
||||
if (debugEnabled) {
|
||||
logger.debug(String.format("Found key '%s' in [%s] with value '%s'", key, propertySource.getName(), value));
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format(
|
||||
"Found key '%s' in [%s] with value '%s'", key, propertySource.getName(), value));
|
||||
}
|
||||
Class<?> clazz;
|
||||
if (value instanceof String) {
|
||||
@@ -131,7 +120,7 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
|
||||
}
|
||||
}
|
||||
else if (value instanceof Class) {
|
||||
clazz = (Class<?>)value;
|
||||
clazz = (Class<?>) value;
|
||||
}
|
||||
else {
|
||||
clazz = value.getClass();
|
||||
@@ -145,14 +134,15 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (debugEnabled) {
|
||||
logger.debug(String.format("Could not find key '%s' in any property source. Returning [null]", key));
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format("Could not find key '%s' in any property source", key));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
@Deprecated
|
||||
private static class ClassConversionException extends ConversionException {
|
||||
|
||||
public ClassConversionException(Class<?> actual, Class<?> expected) {
|
||||
|
||||
Reference in New Issue
Block a user