diff --git a/spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java b/spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java index c44f5d622f4..25268f848db 100644 --- a/spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java +++ b/spring-core/src/main/java/org/springframework/core/env/AbstractEnvironment.java @@ -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 Class getPropertyAsClass(String key, Class targetType) { return this.propertyResolver.getPropertyAsClass(key, targetType); } diff --git a/spring-core/src/main/java/org/springframework/core/env/AbstractPropertyResolver.java b/spring-core/src/main/java/org/springframework/core/env/AbstractPropertyResolver.java index b5f18271cf4..1c001d962a6 100644 --- a/spring-core/src/main/java/org/springframework/core/env/AbstractPropertyResolver.java +++ b/spring-core/src/main/java/org/springframework/core/env/AbstractPropertyResolver.java @@ -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 Class getPropertyAsClass(String key, Class targetValueType) { + throw new UnsupportedOperationException(); + } + @Override public String getRequiredProperty(String key) throws IllegalStateException { String value = getProperty(key); diff --git a/spring-core/src/main/java/org/springframework/core/env/PropertyResolver.java b/spring-core/src/main/java/org/springframework/core/env/PropertyResolver.java index 17570f77b4f..b7b899301ca 100644 --- a/spring-core/src/main/java/org/springframework/core/env/PropertyResolver.java +++ b/spring-core/src/main/java/org/springframework/core/env/PropertyResolver.java @@ -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 getProperty(String key, Class 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 Class getPropertyAsClass(String key, Class targetType); /** diff --git a/spring-core/src/main/java/org/springframework/core/env/PropertySourcesPropertyResolver.java b/spring-core/src/main/java/org/springframework/core/env/PropertySourcesPropertyResolver.java index edf0b72965b..0334bc8d3ff 100644 --- a/spring-core/src/main/java/org/springframework/core/env/PropertySourcesPropertyResolver.java +++ b/spring-core/src/main/java/org/springframework/core/env/PropertySourcesPropertyResolver.java @@ -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 getProperty(String key, Class 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 Class getPropertyAsClass(String key, Class 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) { diff --git a/spring-core/src/test/java/org/springframework/core/env/DummyEnvironment.java b/spring-core/src/test/java/org/springframework/core/env/DummyEnvironment.java index bcb3d2258ef..756da6a94bc 100644 --- a/spring-core/src/test/java/org/springframework/core/env/DummyEnvironment.java +++ b/spring-core/src/test/java/org/springframework/core/env/DummyEnvironment.java @@ -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. @@ -44,6 +44,7 @@ public class DummyEnvironment implements Environment { } @Override + @Deprecated public Class getPropertyAsClass(String key, Class targetType) { return null; } @@ -54,8 +55,7 @@ public class DummyEnvironment implements Environment { } @Override - public T getRequiredProperty(String key, Class targetType) - throws IllegalStateException { + public T getRequiredProperty(String key, Class targetType) throws IllegalStateException { return null; } @@ -65,8 +65,7 @@ public class DummyEnvironment implements Environment { } @Override - public String resolveRequiredPlaceholders(String text) - throws IllegalArgumentException { + public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException { return null; } diff --git a/spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java b/spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java index 84f6fce08a7..35b4e9cb1b9 100644 --- a/spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java +++ b/spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java @@ -24,6 +24,7 @@ import org.junit.Before; import org.junit.Test; import org.springframework.core.convert.ConversionException; +import org.springframework.core.convert.ConverterNotFoundException; import org.springframework.mock.env.MockPropertySource; import static org.hamcrest.Matchers.*; @@ -114,9 +115,9 @@ public class PropertySourcesPropertyResolverTests { try { 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 } } @@ -272,7 +273,7 @@ public class PropertySourcesPropertyResolverTests { assertTrue(resolver.getPropertyAsClass("some.class", SomeType.class).equals(SomeType.class)); } - @Test(expected=ConversionException.class) + @Test(expected = ConversionException.class) public void getPropertyAsClass_withMismatchedTypeForValue() { MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(new MockPropertySource().withProperty("some.class", "java.lang.String")); @@ -280,7 +281,7 @@ public class PropertySourcesPropertyResolverTests { resolver.getPropertyAsClass("some.class", SomeType.class); } - @Test(expected=ConversionException.class) + @Test(expected = ConversionException.class) public void getPropertyAsClass_withNonExistentClassForValue() { MutablePropertySources propertySources = new MutablePropertySources(); 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)); } - @Test(expected=ConversionException.class) + @Test(expected = ConversionException.class) public void getPropertyAsClass_withMismatchedObjectForValue() { MutablePropertySources propertySources = new MutablePropertySources(); 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)); } - @Test(expected=ConversionException.class) + @Test(expected = ConversionException.class) public void getPropertyAsClass_withMismatchedRealClassForValue() { MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(new MockPropertySource().withProperty("some.class", Integer.class));