Add default-value getProperty convenience variants
Issue: SPR-8322
This commit is contained in:
parent
693204aef8
commit
dc2d5c107f
|
|
@ -192,10 +192,18 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||||
return this.propertyResolver.getProperty(key);
|
return this.propertyResolver.getProperty(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getProperty(String key, String defaultValue) {
|
||||||
|
return this.propertyResolver.getProperty(key, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
public <T> T getProperty(String key, Class<T> targetType) {
|
public <T> T getProperty(String key, Class<T> targetType) {
|
||||||
return this.propertyResolver.getProperty(key, targetType);
|
return this.propertyResolver.getProperty(key, targetType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
|
||||||
|
return this.propertyResolver.getProperty(key, targetType, defaultValue);
|
||||||
|
};
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,17 +30,44 @@ public interface PropertyResolver {
|
||||||
boolean containsProperty(String key);
|
boolean containsProperty(String key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the property value associated with the given key.
|
* 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)
|
* @see #getProperty(String, Class)
|
||||||
|
* @see #getRequiredProperty(String)
|
||||||
*/
|
*/
|
||||||
String getProperty(String key);
|
String getProperty(String key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 defaultValue the default value to return if no value is found
|
||||||
|
* @see #getRequiredProperty(String)
|
||||||
|
* @see #getProperty(String, Class)
|
||||||
|
*/
|
||||||
|
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, or {@code null}
|
||||||
* if the key cannot be resolved.
|
* if the key cannot be resolved.
|
||||||
|
* @param key the property name to resolve
|
||||||
|
* @param T the expected type of the property value
|
||||||
|
* @see #getRequiredProperty(String, Class)
|
||||||
*/
|
*/
|
||||||
<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
|
||||||
|
* {@code defaultValue} if the key cannot be resolved.
|
||||||
|
* @param key the property name to resolve
|
||||||
|
* @param T the expected type of the property value
|
||||||
|
* @param defaultValue the default value to return if no value is found
|
||||||
|
* @see #getRequiredProperty(String, Class)
|
||||||
|
*/
|
||||||
|
<T> T getProperty(String string, Class<T> targetType, T defaultValue);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,11 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
|
||||||
return this.getProperty(key, String.class);
|
return this.getProperty(key, String.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getProperty(String key, String defaultValue) {
|
||||||
|
String value = getProperty(key);
|
||||||
|
return value == null ? defaultValue : value;
|
||||||
|
}
|
||||||
|
|
||||||
public <T> T getProperty(String key, Class<T> targetValueType) {
|
public <T> T getProperty(String key, Class<T> targetValueType) {
|
||||||
boolean debugEnabled = logger.isDebugEnabled();
|
boolean debugEnabled = logger.isDebugEnabled();
|
||||||
if (logger.isTraceEnabled()) {
|
if (logger.isTraceEnabled()) {
|
||||||
|
|
@ -89,6 +94,11 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
|
||||||
|
T value = getProperty(key, targetType);
|
||||||
|
return value == null ? defaultValue : value;
|
||||||
|
};
|
||||||
|
|
||||||
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetValueType) {
|
public <T> Class<T> getPropertyAsClass(String key, Class<T> targetValueType) {
|
||||||
boolean debugEnabled = logger.isDebugEnabled();
|
boolean debugEnabled = logger.isDebugEnabled();
|
||||||
if (logger.isTraceEnabled()) {
|
if (logger.isTraceEnabled()) {
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,13 @@ public class PropertySourcesPropertyResolverTests {
|
||||||
assertThat(propertyResolver.getProperty("foo"), is("bar"));
|
assertThat(propertyResolver.getProperty("foo"), is("bar"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getProperty_withDefaultValue() {
|
||||||
|
assertThat(propertyResolver.getProperty("foo", "myDefault"), is("myDefault"));
|
||||||
|
testProperties.put("foo", "bar");
|
||||||
|
assertThat(propertyResolver.getProperty("foo"), is("bar"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getProperty_propertySourceSearchOrderIsFIFO() {
|
public void getProperty_propertySourceSearchOrderIsFIFO() {
|
||||||
MutablePropertySources sources = new MutablePropertySources();
|
MutablePropertySources sources = new MutablePropertySources();
|
||||||
|
|
@ -86,6 +93,13 @@ public class PropertySourcesPropertyResolverTests {
|
||||||
assertThat(propertyResolver.getProperty("foo"), nullValue());
|
assertThat(propertyResolver.getProperty("foo"), nullValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getProperty_withTargetType_andDefaultValue() {
|
||||||
|
assertThat(propertyResolver.getProperty("foo", Integer.class, 42), equalTo(42));
|
||||||
|
testProperties.put("foo", 13);
|
||||||
|
assertThat(propertyResolver.getProperty("foo", Integer.class, 42), equalTo(13));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getProperty_withStringArrayConversion() {
|
public void getProperty_withStringArrayConversion() {
|
||||||
testProperties.put("foo", "bar,baz");
|
testProperties.put("foo", "bar,baz");
|
||||||
|
|
@ -197,7 +211,7 @@ public class PropertySourcesPropertyResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void resolvePlaceholders_withDefault() {
|
public void resolvePlaceholders_withDefaultValue() {
|
||||||
MutablePropertySources propertySources = new MutablePropertySources();
|
MutablePropertySources propertySources = new MutablePropertySources();
|
||||||
propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
|
propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
|
||||||
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
|
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
|
||||||
|
|
@ -227,7 +241,7 @@ public class PropertySourcesPropertyResolverTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void resolveRequiredPlaceholders_withDefault() {
|
public void resolveRequiredPlaceholders_withDefaultValue() {
|
||||||
MutablePropertySources propertySources = new MutablePropertySources();
|
MutablePropertySources propertySources = new MutablePropertySources();
|
||||||
propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
|
propertySources.addFirst(new MockPropertySource().withProperty("key", "value"));
|
||||||
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
|
PropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue