Class AbstractPropertyResolver
- java.lang.Object
- org.springframework.core.env.AbstractPropertyResolver
- All Implemented Interfaces:
ConfigurablePropertyResolver,PropertyResolver
- Direct Known Subclasses:
PropertySourcesPropertyResolver
public abstract class AbstractPropertyResolver extends Object implements ConfigurablePropertyResolver
Abstract base class for resolving properties against any underlying source.- Since:
- 3.1
- Author:
- Chris Beams, Juergen Hoeller
Constructor Summary
Constructors Constructor Description AbstractPropertyResolver()
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleancontainsProperty(String key)Return whether the given property key is available for resolution, i.e.protected <T> TconvertValueIfNecessary(Object value, Class<T> targetType)Convert the given value to the specified target type, if necessary.ConfigurableConversionServicegetConversionService()Return theConfigurableConversionServiceused when performing type conversions on properties.StringgetProperty(String key)Return the property value associated with the given key, ornullif the key cannot be resolved.<T> TgetProperty(String key, Class<T> targetType, T defaultValue)Return the property value associated with the given key, ordefaultValueif the key cannot be resolved.StringgetProperty(String key, String defaultValue)Return the property value associated with the given key, ordefaultValueif the key cannot be resolved.<T> Class<T>getPropertyAsClass(String key, Class<T> targetValueType)Deprecated.protected abstract StringgetPropertyAsRawString(String key)Retrieve the specified property as a raw String, i.e.StringgetRequiredProperty(String key)Return the property value associated with the given key (nevernull).<T> TgetRequiredProperty(String key, Class<T> valueType)Return the property value associated with the given key, converted to the given targetType (nevernull).protected StringresolveNestedPlaceholders(String value)Resolve placeholders within the given string, deferring to the value ofsetIgnoreUnresolvableNestedPlaceholders(boolean)to determine whether any unresolvable placeholders should raise an exception or be ignored.StringresolvePlaceholders(String text)Resolve ${...} placeholders in the given text, replacing them with corresponding property values as resolved byPropertyResolver.getProperty(java.lang.String).StringresolveRequiredPlaceholders(String text)Resolve ${...} placeholders in the given text, replacing them with corresponding property values as resolved byPropertyResolver.getProperty(java.lang.String).voidsetConversionService(ConfigurableConversionService conversionService)Set theConfigurableConversionServiceto be used when performing type conversions on properties.voidsetIgnoreUnresolvableNestedPlaceholders(boolean ignoreUnresolvableNestedPlaceholders)Set whether to throw an exception when encountering an unresolvable placeholder nested within the value of a given property.voidsetPlaceholderPrefix(String placeholderPrefix)Set the prefix that placeholders replaced by this resolver must begin with.voidsetPlaceholderSuffix(String placeholderSuffix)Set the suffix that placeholders replaced by this resolver must end with.voidsetRequiredProperties(String... requiredProperties)Specify which properties must be present, to be verified byConfigurablePropertyResolver.validateRequiredProperties().voidsetValueSeparator(String valueSeparator)Specify the separating character between the placeholders replaced by this resolver and their associated default value, ornullif no such special character should be processed as a value separator.voidvalidateRequiredProperties()Validate that each of the properties specified byConfigurablePropertyResolver.setRequiredProperties(java.lang.String...)is present and resolves to a non-nullvalue.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.core.env.PropertyResolver
getProperty
Constructor Detail
AbstractPropertyResolver
public AbstractPropertyResolver()
Method Detail
getConversionService
public ConfigurableConversionService getConversionService()
Description copied from interface:ConfigurablePropertyResolverReturn theConfigurableConversionServiceused when performing type conversions on properties.The configurable nature of the returned conversion service allows for the convenient addition and removal of individual
Converterinstances:ConfigurableConversionService cs = env.getConversionService(); cs.addConverter(new FooConverter());
setConversionService
public void setConversionService(ConfigurableConversionService conversionService)
Description copied from interface:ConfigurablePropertyResolverSet theConfigurableConversionServiceto be used when performing type conversions on properties.Note: as an alternative to fully replacing the
ConversionService, consider adding or removing individualConverterinstances by drilling intoConfigurablePropertyResolver.getConversionService()and calling methods such as#addConverter.
setPlaceholderPrefix
public void setPlaceholderPrefix(String placeholderPrefix)
Set the prefix that placeholders replaced by this resolver must begin with.The default is "${".
- Specified by:
setPlaceholderPrefixin interfaceConfigurablePropertyResolver- See Also:
SystemPropertyUtils.PLACEHOLDER_PREFIX
setPlaceholderSuffix
public void setPlaceholderSuffix(String placeholderSuffix)
Set the suffix that placeholders replaced by this resolver must end with.The default is "}".
- Specified by:
setPlaceholderSuffixin interfaceConfigurablePropertyResolver- See Also:
SystemPropertyUtils.PLACEHOLDER_SUFFIX
setValueSeparator
public void setValueSeparator(String valueSeparator)
Specify the separating character between the placeholders replaced by this resolver and their associated default value, ornullif no such special character should be processed as a value separator.The default is ":".
- Specified by:
setValueSeparatorin interfaceConfigurablePropertyResolver- See Also:
SystemPropertyUtils.VALUE_SEPARATOR
setIgnoreUnresolvableNestedPlaceholders
public void setIgnoreUnresolvableNestedPlaceholders(boolean ignoreUnresolvableNestedPlaceholders)
Set whether to throw an exception when encountering an unresolvable placeholder nested within the value of a given property. Afalsevalue indicates strict resolution, i.e. that an exception will be thrown. Atruevalue indicates that unresolvable nested placeholders should be passed through in their unresolved ${...} form.The default is
false.- Specified by:
setIgnoreUnresolvableNestedPlaceholdersin interfaceConfigurablePropertyResolver- Since:
- 3.2
setRequiredProperties
public void setRequiredProperties(String... requiredProperties)
Description copied from interface:ConfigurablePropertyResolverSpecify which properties must be present, to be verified byConfigurablePropertyResolver.validateRequiredProperties().- Specified by:
setRequiredPropertiesin interfaceConfigurablePropertyResolver
validateRequiredProperties
public void validateRequiredProperties()
Description copied from interface:ConfigurablePropertyResolverValidate that each of the properties specified byConfigurablePropertyResolver.setRequiredProperties(java.lang.String...)is present and resolves to a non-nullvalue.- Specified by:
validateRequiredPropertiesin interfaceConfigurablePropertyResolver
containsProperty
public boolean containsProperty(String key)
Description copied from interface:PropertyResolverReturn whether the given property key is available for resolution, i.e. if the value for the given key is notnull.- Specified by:
containsPropertyin interfacePropertyResolver
getProperty
public String getProperty(String key)
Description copied from interface:PropertyResolverReturn the property value associated with the given key, ornullif the key cannot be resolved.- Specified by:
getPropertyin interfacePropertyResolver- Parameters:
key- the property name to resolve- See Also:
PropertyResolver.getProperty(String, String),PropertyResolver.getProperty(String, Class),PropertyResolver.getRequiredProperty(String)
getProperty
public String getProperty(String key, String defaultValue)
Description copied from interface:PropertyResolverReturn the property value associated with the given key, ordefaultValueif the key cannot be resolved.- Specified by:
getPropertyin interfacePropertyResolver- Parameters:
key- the property name to resolvedefaultValue- the default value to return if no value is found- See Also:
PropertyResolver.getRequiredProperty(String),PropertyResolver.getProperty(String, Class)
getProperty
public <T> T getProperty(String key, Class<T> targetType, T defaultValue)
Description copied from interface:PropertyResolverReturn the property value associated with the given key, ordefaultValueif the key cannot be resolved.- Specified by:
getPropertyin interfacePropertyResolver- Parameters:
key- the property name to resolvetargetType- the expected type of the property valuedefaultValue- the default value to return if no value is found- See Also:
PropertyResolver.getRequiredProperty(String, Class)
getPropertyAsClass
@Deprecated public <T> Class<T> getPropertyAsClass(String key, Class<T> targetValueType)
Deprecated.Description copied from interface:PropertyResolverConvert the property value associated with the given key to aClassof typeTornullif the key cannot be resolved.- Specified by:
getPropertyAsClassin interfacePropertyResolver- See Also:
PropertyResolver.getProperty(String, Class)
getRequiredProperty
public String getRequiredProperty(String key) throws IllegalStateException
Description copied from interface:PropertyResolverReturn the property value associated with the given key (nevernull).- Specified by:
getRequiredPropertyin interfacePropertyResolver- Throws:
IllegalStateException- if the key cannot be resolved- See Also:
PropertyResolver.getRequiredProperty(String, Class)
getRequiredProperty
public <T> T getRequiredProperty(String key, Class<T> valueType) throws IllegalStateException
Description copied from interface:PropertyResolverReturn the property value associated with the given key, converted to the given targetType (nevernull).- Specified by:
getRequiredPropertyin interfacePropertyResolver- Throws:
IllegalStateException- if the given key cannot be resolved
resolvePlaceholders
public String resolvePlaceholders(String text)
Description copied from interface:PropertyResolverResolve ${...} placeholders in the given text, replacing them with corresponding property values as resolved byPropertyResolver.getProperty(java.lang.String). Unresolvable placeholders with no default value are ignored and passed through unchanged.- Specified by:
resolvePlaceholdersin interfacePropertyResolver- Parameters:
text- the String to resolve- Returns:
- the resolved String (never
null) - See Also:
PropertyResolver.resolveRequiredPlaceholders(java.lang.String),SystemPropertyUtils.resolvePlaceholders(String)
resolveRequiredPlaceholders
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException
Description copied from interface:PropertyResolverResolve ${...} placeholders in the given text, replacing them with corresponding property values as resolved byPropertyResolver.getProperty(java.lang.String). Unresolvable placeholders with no default value will cause an IllegalArgumentException to be thrown.- Specified by:
resolveRequiredPlaceholdersin interfacePropertyResolver- Returns:
- the resolved String (never
null) - Throws:
IllegalArgumentException- if given text isnullor if any placeholders are unresolvable- See Also:
SystemPropertyUtils.resolvePlaceholders(String, boolean)
resolveNestedPlaceholders
protected String resolveNestedPlaceholders(String value)
Resolve placeholders within the given string, deferring to the value ofsetIgnoreUnresolvableNestedPlaceholders(boolean)to determine whether any unresolvable placeholders should raise an exception or be ignored.Invoked from
getProperty(java.lang.String)and its variants, implicitly resolving nested placeholders. In contrast,resolvePlaceholders(java.lang.String)andresolveRequiredPlaceholders(java.lang.String)donot delegate to this method but rather perform their own handling of unresolvable placeholders, as specified by each of those methods.- Since:
- 3.2
- See Also:
setIgnoreUnresolvableNestedPlaceholders(boolean)
convertValueIfNecessary
protected <T> T convertValueIfNecessary(Object value, Class<T> targetType)
Convert the given value to the specified target type, if necessary.- Parameters:
value- the original property valuetargetType- the specified target type for property retrieval- Returns:
- the converted value, or the original value if no conversion is necessary
- Since:
- 4.3.5
getPropertyAsRawString
protected abstract String getPropertyAsRawString(String key)
Retrieve the specified property as a raw String, i.e. without resolution of nested placeholders.- Parameters:
key- the property name to resolve- Returns:
- the property value or
nullif none found