Spring Framework

org.springframework.core.env
Class AbstractPropertyResolver

java.lang.Object
  extended by org.springframework.core.env.AbstractPropertyResolver
All Implemented Interfaces:
ConfigurablePropertyResolver, PropertyResolver
Direct Known Subclasses:
PropertySourcesPropertyResolver

public abstract class AbstractPropertyResolver
extends java.lang.Object
implements ConfigurablePropertyResolver

Abstract base class for resolving properties against any underlying source.

Since:
3.1
Author:
Chris Beams

Field Summary
protected  ConfigurableConversionService conversionService
           
protected  org.apache.commons.logging.Log logger
           
 
Constructor Summary
AbstractPropertyResolver()
           
 
Method Summary
 ConfigurableConversionService getConversionService()
           
<T> T
getProperty(java.lang.String key, java.lang.Class<T> targetType, T defaultValue)
          Return the property value associated with the given key, or defaultValue if the key cannot be resolved.
 java.lang.String getProperty(java.lang.String key, java.lang.String defaultValue)
          Return the property value associated with the given key, or defaultValue if the key cannot be resolved.
 java.lang.String getRequiredProperty(java.lang.String key)
          Return the property value associated with the given key, converted to the given targetType (never null).
<T> T
getRequiredProperty(java.lang.String key, java.lang.Class<T> valueType)
          Return the property value associated with the given key, converted to the given targetType (never null).
 java.lang.String resolvePlaceholders(java.lang.String text)
          Resolve ${...} placeholders in the given text, replacing them with corresponding property values as resolved by PropertyResolver.getProperty(java.lang.String).
 java.lang.String resolveRequiredPlaceholders(java.lang.String text)
          Resolve ${...} placeholders in the given text, replacing them with corresponding property values as resolved by PropertyResolver.getProperty(java.lang.String).
 void setConversionService(ConfigurableConversionService conversionService)
          Set the ConfigurableConversionService to be used when performing type conversions on properties.
 void setPlaceholderPrefix(java.lang.String placeholderPrefix)
          Set the prefix that placeholders replaced by this resolver must begin with.
 void setPlaceholderSuffix(java.lang.String placeholderSuffix)
          Set the suffix that placeholders replaced by this resolver must end with.
 void setRequiredProperties(java.lang.String... requiredProperties)
          Specify which properties must be present, to be verified by ConfigurablePropertyResolver.validateRequiredProperties().
 void setValueSeparator(java.lang.String valueSeparator)
          Specify the separating character between the placeholders replaced by this resolver and their associated default value, or null if no such special character should be processed as a value separator.
 void validateRequiredProperties()
          Validate that each of the properties specified by ConfigurablePropertyResolver.setRequiredProperties(java.lang.String...) is present and resolves to a non-null value.
 
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
containsProperty, getProperty, getProperty, getPropertyAsClass
 

Field Detail

logger

protected final org.apache.commons.logging.Log logger

conversionService

protected ConfigurableConversionService conversionService
Constructor Detail

AbstractPropertyResolver

public AbstractPropertyResolver()
Method Detail

getConversionService

public ConfigurableConversionService getConversionService()
Specified by:
getConversionService in interface ConfigurablePropertyResolver
Returns:
the ConfigurableConversionService used when performing type conversions on properties.

The configurable nature of the returned conversion service allows for the convenient addition and removal of individual Converter instances:

 ConfigurableConversionService cs = env.getConversionService();
 cs.addConverter(new FooConverter());
 
See Also:
PropertyResolver.getProperty(String, Class), ConverterRegistry.addConverter(org.springframework.core.convert.converter.Converter)

setConversionService

public void setConversionService(ConfigurableConversionService conversionService)
Description copied from interface: ConfigurablePropertyResolver
Set the ConfigurableConversionService to be used when performing type conversions on properties.

Note: as an alternative to fully replacing the ConversionService, consider adding or removing individual Converter instances by drilling into ConfigurablePropertyResolver.getConversionService() and calling methods such as #addConverter.

Specified by:
setConversionService in interface ConfigurablePropertyResolver
See Also:
PropertyResolver.getProperty(String, Class), ConfigurablePropertyResolver.getConversionService(), ConverterRegistry.addConverter(org.springframework.core.convert.converter.Converter)

getProperty

public java.lang.String getProperty(java.lang.String key,
                                    java.lang.String defaultValue)
Description copied from interface: PropertyResolver
Return the property value associated with the given key, or defaultValue if the key cannot be resolved.

Specified by:
getProperty in interface PropertyResolver
Parameters:
key - the property name to resolve
defaultValue - the default value to return if no value is found
See Also:
PropertyResolver.getRequiredProperty(String), PropertyResolver.getProperty(String, Class)

getProperty

public <T> T getProperty(java.lang.String key,
                         java.lang.Class<T> targetType,
                         T defaultValue)
Description copied from interface: PropertyResolver
Return the property value associated with the given key, or defaultValue if the key cannot be resolved.

Specified by:
getProperty in interface PropertyResolver
Parameters:
key - the property name to resolve
targetType - the expected type of the property value
defaultValue - the default value to return if no value is found
See Also:
PropertyResolver.getRequiredProperty(String, Class)

setRequiredProperties

public void setRequiredProperties(java.lang.String... requiredProperties)
Description copied from interface: ConfigurablePropertyResolver
Specify which properties must be present, to be verified by ConfigurablePropertyResolver.validateRequiredProperties().

Specified by:
setRequiredProperties in interface ConfigurablePropertyResolver

validateRequiredProperties

public void validateRequiredProperties()
Description copied from interface: ConfigurablePropertyResolver
Validate that each of the properties specified by ConfigurablePropertyResolver.setRequiredProperties(java.lang.String...) is present and resolves to a non-null value.

Specified by:
validateRequiredProperties in interface ConfigurablePropertyResolver

getRequiredProperty

public java.lang.String getRequiredProperty(java.lang.String key)
                                     throws java.lang.IllegalStateException
Description copied from interface: PropertyResolver
Return the property value associated with the given key, converted to the given targetType (never null).

Specified by:
getRequiredProperty in interface PropertyResolver
Throws:
java.lang.IllegalStateException - if the key cannot be resolved
See Also:
PropertyResolver.getRequiredProperty(String, Class)

getRequiredProperty

public <T> T getRequiredProperty(java.lang.String key,
                                 java.lang.Class<T> valueType)
                      throws java.lang.IllegalStateException
Description copied from interface: PropertyResolver
Return the property value associated with the given key, converted to the given targetType (never null).

Specified by:
getRequiredProperty in interface PropertyResolver
Throws:
java.lang.IllegalStateException - if the given key cannot be resolved

setPlaceholderPrefix

public void setPlaceholderPrefix(java.lang.String placeholderPrefix)
Set the prefix that placeholders replaced by this resolver must begin with. The default is "${".

Specified by:
setPlaceholderPrefix in interface ConfigurablePropertyResolver
See Also:
SystemPropertyUtils.PLACEHOLDER_PREFIX

setPlaceholderSuffix

public void setPlaceholderSuffix(java.lang.String placeholderSuffix)
Set the suffix that placeholders replaced by this resolver must end with. The default is "}".

Specified by:
setPlaceholderSuffix in interface ConfigurablePropertyResolver
See Also:
SystemPropertyUtils.PLACEHOLDER_SUFFIX

setValueSeparator

public void setValueSeparator(java.lang.String valueSeparator)
Specify the separating character between the placeholders replaced by this resolver and their associated default value, or null if no such special character should be processed as a value separator. The default is ":".

Specified by:
setValueSeparator in interface ConfigurablePropertyResolver
See Also:
SystemPropertyUtils.VALUE_SEPARATOR

resolvePlaceholders

public java.lang.String resolvePlaceholders(java.lang.String text)
Description copied from interface: PropertyResolver
Resolve ${...} placeholders in the given text, replacing them with corresponding property values as resolved by PropertyResolver.getProperty(java.lang.String). Unresolvable placeholders with no default value are ignored and passed through unchanged.

Specified by:
resolvePlaceholders in interface PropertyResolver
Parameters:
text - the String to resolve
Returns:
the resolved String (never null)
See Also:
PropertyResolver.resolveRequiredPlaceholders(java.lang.String), SystemPropertyUtils.resolvePlaceholders(String)

resolveRequiredPlaceholders

public java.lang.String resolveRequiredPlaceholders(java.lang.String text)
                                             throws java.lang.IllegalArgumentException
Description copied from interface: PropertyResolver
Resolve ${...} placeholders in the given text, replacing them with corresponding property values as resolved by PropertyResolver.getProperty(java.lang.String). Unresolvable placeholders with no default value will cause an IllegalArgumentException to be thrown.

Specified by:
resolveRequiredPlaceholders in interface PropertyResolver
Returns:
the resolved String (never null)
Throws:
java.lang.IllegalArgumentException - if given text is null
See Also:
SystemPropertyUtils.resolvePlaceholders(String, boolean)

Spring Framework