public abstract class PlaceholderConfigurerSupport extends PropertyResourceConfigurer implements BeanNameAware, BeanFactoryAware
The default placeholder syntax follows the Ant / Log4J / JSP EL style:
${...}Example XML bean definition:
Example properties file:<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"/> <property name="driverClassName" value="
${driver}"/> <property name="url" value="jdbc:
${dbname}"/> </bean>
driver=com.mysql.jdbc.Driver dbname=mysql:mydbAnnotated bean definitions may take advantage of property replacement using the
@Value
annotation:
@Value("${person.age}")Implementations check simple property values, lists, maps, props, and bean names in bean references. Furthermore, placeholder values can also cross-reference other placeholders, like:
rootPath=myrootdir subPath=${rootPath}/subdirIn contrast to
PropertyOverrideConfigurer
, subclasses of this type allow
filling in of explicit placeholders in bean definitions.
If a configurer cannot resolve a placeholder, a BeanDefinitionStoreException
will be thrown. If you want to check against multiple properties files, specify multiple
resources via the locations
property. You can also define multiple
configurers, each with its own placeholder syntax. Use ignoreUnresolvablePlaceholders
to intentionally suppress throwing an exception if a
placeholder cannot be resolved.
Default property values can be defined globally for each configurer instance
via the properties
property, or on a property-by-property basis
using the default value separator which is ":"
by default and
customizable via setValueSeparator(String)
.
Example XML property with default value:
<property name="url" value="jdbc:
${dbname:defaultdb}"/>
PropertyPlaceholderConfigurer
,
PropertySourcesPlaceholderConfigurer
Modifier and Type | Field and Description |
---|---|
private BeanFactory |
beanFactory |
private java.lang.String |
beanName |
static java.lang.String |
DEFAULT_PLACEHOLDER_PREFIX
Default placeholder prefix: "${"
|
static java.lang.String |
DEFAULT_PLACEHOLDER_SUFFIX
Default placeholder suffix: "}"
|
static java.lang.String |
DEFAULT_VALUE_SEPARATOR
Default value separator: ":"
|
protected boolean |
ignoreUnresolvablePlaceholders |
protected java.lang.String |
nullValue |
protected java.lang.String |
placeholderPrefix
Defaults to "${"
|
protected java.lang.String |
placeholderSuffix
Defaults to "}"
|
protected java.lang.String |
valueSeparator
Defaults to ":"
|
localOverride, localProperties, logger
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor and Description |
---|
PlaceholderConfigurerSupport() |
Modifier and Type | Method and Description |
---|---|
protected void |
doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
StringValueResolver valueResolver) |
void |
setBeanFactory(BeanFactory beanFactory)
Only necessary to check that we're not parsing our own bean definition,
to avoid failing on unresolvable placeholders in properties file locations.
|
void |
setBeanName(java.lang.String beanName)
Only necessary to check that we're not parsing our own bean definition,
to avoid failing on unresolvable placeholders in properties file locations.
|
void |
setIgnoreUnresolvablePlaceholders(boolean ignoreUnresolvablePlaceholders)
Set whether to ignore unresolvable placeholders.
|
void |
setNullValue(java.lang.String nullValue)
Set a value that should be treated as
null when
resolved as a placeholder value: e.g. |
void |
setPlaceholderPrefix(java.lang.String placeholderPrefix)
Set the prefix that a placeholder string starts with.
|
void |
setPlaceholderSuffix(java.lang.String placeholderSuffix)
Set the suffix that a placeholder string ends with.
|
void |
setValueSeparator(java.lang.String valueSeparator)
Specify the separating character between the placeholder variable
and the associated default value, or
null if no such
special character should be processed as a value separator. |
convertProperties, convertProperty, convertPropertyValue, getOrder, postProcessBeanFactory, processProperties, setOrder
loadProperties, mergeProperties, setFileEncoding, setIgnoreResourceNotFound, setLocalOverride, setLocation, setLocations, setProperties, setPropertiesArray, setPropertiesPersister
public static final java.lang.String DEFAULT_PLACEHOLDER_PREFIX
public static final java.lang.String DEFAULT_PLACEHOLDER_SUFFIX
public static final java.lang.String DEFAULT_VALUE_SEPARATOR
protected java.lang.String placeholderPrefix
protected java.lang.String placeholderSuffix
protected java.lang.String valueSeparator
protected boolean ignoreUnresolvablePlaceholders
protected java.lang.String nullValue
private BeanFactory beanFactory
private java.lang.String beanName
public void setPlaceholderPrefix(java.lang.String placeholderPrefix)
public void setPlaceholderSuffix(java.lang.String placeholderSuffix)
public void setValueSeparator(java.lang.String valueSeparator)
null
if no such
special character should be processed as a value separator.
The default is ":".public void setNullValue(java.lang.String nullValue)
null
when
resolved as a placeholder value: e.g. "" (empty String) or "null".
Note that this will only apply to full property values, not to parts of concatenated values.
By default, no such null value is defined. This means that
there is no way to express null
as a property
value unless you explicitly map a corresponding value here.
public void setIgnoreUnresolvablePlaceholders(boolean ignoreUnresolvablePlaceholders)
Default is "false": An exception will be thrown if a placeholder fails to resolve. Switch this flag to "true" in order to preserve the placeholder String as-is in such a case, leaving it up to other placeholder configurers to resolve it.
public void setBeanName(java.lang.String beanName)
setBeanName
in interface BeanNameAware
beanName
- the name of the bean in the factory.
Note that this name is the actual bean name used in the factory, which may
differ from the originally specified name: in particular for inner bean
names, the actual bean name might have been made unique through appending
"#..." suffixes. Use the BeanFactoryUtils.originalBeanName(String)
method to extract the original bean name (without suffix), if desired.PropertiesLoaderSupport.setLocations(org.springframework.core.io.Resource[])
,
ResourceEditor
public void setBeanFactory(BeanFactory beanFactory)
setBeanFactory
in interface BeanFactoryAware
beanFactory
- owning BeanFactory (never null
).
The bean can immediately call methods on the factory.PropertiesLoaderSupport.setLocations(org.springframework.core.io.Resource[])
,
ResourceEditor
protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess, StringValueResolver valueResolver)