Deprecated getPropertyAsClass and refined PropertySourcesPropertyResolver's logging
Issue: SPR-14370
This commit is contained in:
parent
37e42e68e8
commit
1d42009c0a
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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
|
@Override
|
||||||
|
@Deprecated
|
||||||
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetType) {
|
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetType) {
|
||||||
return this.propertyResolver.getPropertyAsClass(key, 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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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
|
@Override
|
||||||
public String getProperty(String key, String defaultValue) {
|
public String getProperty(String key, String defaultValue) {
|
||||||
|
|
@ -144,6 +153,12 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe
|
||||||
return (value != null ? value : defaultValue);
|
return (value != null ? value : defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetValueType) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getRequiredProperty(String key) throws IllegalStateException {
|
public String getRequiredProperty(String key) throws IllegalStateException {
|
||||||
String value = getProperty(key);
|
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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.
|
* Interface for resolving properties against any underlying source.
|
||||||
*
|
*
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
|
* @author Juergen Hoeller
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
* @see Environment
|
* @see Environment
|
||||||
* @see PropertySourcesPropertyResolver
|
* @see PropertySourcesPropertyResolver
|
||||||
|
|
@ -27,14 +28,14 @@ package org.springframework.core.env;
|
||||||
public interface PropertyResolver {
|
public interface PropertyResolver {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return whether the given property key is available for resolution, i.e.,
|
* Return whether the given property key is available for resolution,
|
||||||
* the value for the given key is not {@code null}.
|
* i.e. if the value for the given key is not {@code null}.
|
||||||
*/
|
*/
|
||||||
boolean containsProperty(String key);
|
boolean containsProperty(String key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the property value associated with the given key, or {@code null}
|
* Return the property value associated with the given key,
|
||||||
* if the key cannot be resolved.
|
* or {@code null} if the key cannot be resolved.
|
||||||
* @param key the property name to resolve
|
* @param key the property name to resolve
|
||||||
* @see #getProperty(String, String)
|
* @see #getProperty(String, String)
|
||||||
* @see #getProperty(String, Class)
|
* @see #getProperty(String, Class)
|
||||||
|
|
@ -53,8 +54,8 @@ public interface PropertyResolver {
|
||||||
String getProperty(String key, String defaultValue);
|
String getProperty(String key, String defaultValue);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the property value associated with the given key, or {@code null}
|
* Return the property value associated with the given key,
|
||||||
* if the key cannot be resolved.
|
* or {@code null} if the key cannot be resolved.
|
||||||
* @param key the property name to resolve
|
* @param key the property name to resolve
|
||||||
* @param targetType the expected type of the property value
|
* @param targetType the expected type of the property value
|
||||||
* @see #getRequiredProperty(String, Class)
|
* @see #getRequiredProperty(String, Class)
|
||||||
|
|
@ -62,8 +63,8 @@ public interface PropertyResolver {
|
||||||
<T> T getProperty(String key, Class<T> targetType);
|
<T> T getProperty(String key, Class<T> targetType);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the property value associated with the given key, or
|
* Return the property value associated with the given key,
|
||||||
* {@code defaultValue} if the key cannot be resolved.
|
* or {@code defaultValue} if the key cannot be resolved.
|
||||||
* @param key the property name to resolve
|
* @param key the property name to resolve
|
||||||
* @param targetType the expected type of the property value
|
* @param targetType the expected type of the property value
|
||||||
* @param defaultValue the default value to return if no value is found
|
* @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}
|
* 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.
|
* of type {@code T} or {@code null} if the key cannot be resolved.
|
||||||
* @throws org.springframework.core.convert.ConversionException if class specified
|
* @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
|
* from class specified by property value
|
||||||
* @see #getProperty(String, Class)
|
* @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);
|
<T> Class<T> getPropertyAsClass(String key, Class<T> targetType);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ import org.springframework.util.ClassUtils;
|
||||||
* an underlying set of {@link PropertySources}.
|
* an underlying set of {@link PropertySources}.
|
||||||
*
|
*
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
|
* @author Juergen Hoeller
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
* @see PropertySource
|
* @see PropertySource
|
||||||
* @see PropertySources
|
* @see PropertySources
|
||||||
|
|
@ -71,55 +72,43 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) {
|
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) {
|
if (this.propertySources != null) {
|
||||||
for (PropertySource<?> propertySource : this.propertySources) {
|
for (PropertySource<?> propertySource : this.propertySources) {
|
||||||
if (debugEnabled) {
|
if (logger.isTraceEnabled()) {
|
||||||
logger.debug(String.format("Searching for key '%s' in [%s]", key, propertySource.getName()));
|
logger.trace(String.format("Searching for key '%s' in [%s]", key, propertySource.getName()));
|
||||||
}
|
}
|
||||||
Object value = propertySource.getProperty(key);
|
Object value = propertySource.getProperty(key);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
Class<?> valueType = value.getClass();
|
|
||||||
if (resolveNestedPlaceholders && value instanceof String) {
|
if (resolveNestedPlaceholders && value instanceof String) {
|
||||||
value = resolveNestedPlaceholders((String) value);
|
value = resolveNestedPlaceholders((String) value);
|
||||||
}
|
}
|
||||||
if (debugEnabled) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(String.format("Found key '%s' in [%s] with type [%s] and value '%s'",
|
logger.debug(String.format("Found key '%s' in [%s] with type [%s] and value '%s'",
|
||||||
key, propertySource.getName(), valueType.getSimpleName(), value));
|
key, propertySource.getName(), value.getClass().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()));
|
|
||||||
}
|
}
|
||||||
return this.conversionService.convert(value, targetValueType);
|
return this.conversionService.convert(value, targetValueType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (debugEnabled) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(String.format("Could not find key '%s' in any property source. Returning [null]", key));
|
logger.debug(String.format("Could not find key '%s' in any property source", key));
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Deprecated
|
||||||
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetValueType) {
|
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) {
|
if (this.propertySources != null) {
|
||||||
for (PropertySource<?> propertySource : this.propertySources) {
|
for (PropertySource<?> propertySource : this.propertySources) {
|
||||||
if (debugEnabled) {
|
if (logger.isTraceEnabled()) {
|
||||||
logger.debug(String.format("Searching for key '%s' in [%s]", key, propertySource.getName()));
|
logger.trace(String.format("Searching for key '%s' in [%s]", key, propertySource.getName()));
|
||||||
}
|
}
|
||||||
Object value = propertySource.getProperty(key);
|
Object value = propertySource.getProperty(key);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
if (debugEnabled) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(String.format("Found key '%s' in [%s] with value '%s'", key, propertySource.getName(), value));
|
logger.debug(String.format(
|
||||||
|
"Found key '%s' in [%s] with value '%s'", key, propertySource.getName(), value));
|
||||||
}
|
}
|
||||||
Class<?> clazz;
|
Class<?> clazz;
|
||||||
if (value instanceof String) {
|
if (value instanceof String) {
|
||||||
|
|
@ -131,7 +120,7 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (value instanceof Class) {
|
else if (value instanceof Class) {
|
||||||
clazz = (Class<?>)value;
|
clazz = (Class<?>) value;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
clazz = value.getClass();
|
clazz = value.getClass();
|
||||||
|
|
@ -145,14 +134,15 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (debugEnabled) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(String.format("Could not find key '%s' in any property source. Returning [null]", key));
|
logger.debug(String.format("Could not find key '%s' in any property source", key));
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
|
@Deprecated
|
||||||
private static class ClassConversionException extends ConversionException {
|
private static class ClassConversionException extends ConversionException {
|
||||||
|
|
||||||
public ClassConversionException(Class<?> actual, Class<?> expected) {
|
public ClassConversionException(Class<?> actual, Class<?> expected) {
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -44,6 +44,7 @@ public class DummyEnvironment implements Environment {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Deprecated
|
||||||
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetType) {
|
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetType) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
@ -54,8 +55,7 @@ public class DummyEnvironment implements Environment {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T getRequiredProperty(String key, Class<T> targetType)
|
public <T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException {
|
||||||
throws IllegalStateException {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -65,8 +65,7 @@ public class DummyEnvironment implements Environment {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String resolveRequiredPlaceholders(String text)
|
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
|
||||||
throws IllegalArgumentException {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.core.convert.ConversionException;
|
import org.springframework.core.convert.ConversionException;
|
||||||
|
import org.springframework.core.convert.ConverterNotFoundException;
|
||||||
import org.springframework.mock.env.MockPropertySource;
|
import org.springframework.mock.env.MockPropertySource;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.*;
|
import static org.hamcrest.Matchers.*;
|
||||||
|
|
@ -114,9 +115,9 @@ public class PropertySourcesPropertyResolverTests {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
propertyResolver.getProperty("foo", TestType.class);
|
propertyResolver.getProperty("foo", TestType.class);
|
||||||
fail("Expected IllegalArgumentException due to non-convertible types");
|
fail("Expected ConverterNotFoundException due to non-convertible types");
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException ex) {
|
catch (ConverterNotFoundException ex) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -272,7 +273,7 @@ public class PropertySourcesPropertyResolverTests {
|
||||||
assertTrue(resolver.getPropertyAsClass("some.class", SomeType.class).equals(SomeType.class));
|
assertTrue(resolver.getPropertyAsClass("some.class", SomeType.class).equals(SomeType.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=ConversionException.class)
|
@Test(expected = ConversionException.class)
|
||||||
public void getPropertyAsClass_withMismatchedTypeForValue() {
|
public void getPropertyAsClass_withMismatchedTypeForValue() {
|
||||||
MutablePropertySources propertySources = new MutablePropertySources();
|
MutablePropertySources propertySources = new MutablePropertySources();
|
||||||
propertySources.addFirst(new MockPropertySource().withProperty("some.class", "java.lang.String"));
|
propertySources.addFirst(new MockPropertySource().withProperty("some.class", "java.lang.String"));
|
||||||
|
|
@ -280,7 +281,7 @@ public class PropertySourcesPropertyResolverTests {
|
||||||
resolver.getPropertyAsClass("some.class", SomeType.class);
|
resolver.getPropertyAsClass("some.class", SomeType.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=ConversionException.class)
|
@Test(expected = ConversionException.class)
|
||||||
public void getPropertyAsClass_withNonExistentClassForValue() {
|
public void getPropertyAsClass_withNonExistentClassForValue() {
|
||||||
MutablePropertySources propertySources = new MutablePropertySources();
|
MutablePropertySources propertySources = new MutablePropertySources();
|
||||||
propertySources.addFirst(new MockPropertySource().withProperty("some.class", "some.bogus.Class"));
|
propertySources.addFirst(new MockPropertySource().withProperty("some.class", "some.bogus.Class"));
|
||||||
|
|
@ -296,7 +297,7 @@ public class PropertySourcesPropertyResolverTests {
|
||||||
assertTrue(resolver.getPropertyAsClass("some.class", SomeType.class).equals(SpecificType.class));
|
assertTrue(resolver.getPropertyAsClass("some.class", SomeType.class).equals(SpecificType.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=ConversionException.class)
|
@Test(expected = ConversionException.class)
|
||||||
public void getPropertyAsClass_withMismatchedObjectForValue() {
|
public void getPropertyAsClass_withMismatchedObjectForValue() {
|
||||||
MutablePropertySources propertySources = new MutablePropertySources();
|
MutablePropertySources propertySources = new MutablePropertySources();
|
||||||
propertySources.addFirst(new MockPropertySource().withProperty("some.class", new Integer(42)));
|
propertySources.addFirst(new MockPropertySource().withProperty("some.class", new Integer(42)));
|
||||||
|
|
@ -312,7 +313,7 @@ public class PropertySourcesPropertyResolverTests {
|
||||||
assertTrue(resolver.getPropertyAsClass("some.class", SomeType.class).equals(SpecificType.class));
|
assertTrue(resolver.getPropertyAsClass("some.class", SomeType.class).equals(SpecificType.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=ConversionException.class)
|
@Test(expected = ConversionException.class)
|
||||||
public void getPropertyAsClass_withMismatchedRealClassForValue() {
|
public void getPropertyAsClass_withMismatchedRealClassForValue() {
|
||||||
MutablePropertySources propertySources = new MutablePropertySources();
|
MutablePropertySources propertySources = new MutablePropertySources();
|
||||||
propertySources.addFirst(new MockPropertySource().withProperty("some.class", Integer.class));
|
propertySources.addFirst(new MockPropertySource().withProperty("some.class", Integer.class));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue