org.springframework.beans.factory.config
public class: PropertyOverrideConfigurer [javadoc |
source]
java.lang.Object
org.springframework.core.io.support.PropertiesLoaderSupport
org.springframework.beans.factory.config.PropertyResourceConfigurer
org.springframework.beans.factory.config.PropertyOverrideConfigurer
All Implemented Interfaces:
PriorityOrdered, BeanFactoryPostProcessor
Property resource configurer that overrides bean property values in an application
context definition. It
pushes values from a properties file into bean definitions.
Configuration lines are expected to be of the following form:
beanName.property=value
Example properties file:
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql:mydb
In contrast to PropertyPlaceholderConfigurer, the original definition can have default
values or no values at all for such bean properties. If an overriding properties file does
not have an entry for a certain bean property, the default context definition is used.
Note that the context definition is not aware of being overridden;
so this is not immediately obvious when looking at the XML definition file.
In case of multiple PropertyOverrideConfigurers that define different values for
the same bean property, the last one will win (due to the overriding mechanism).
Property values can be converted after reading them in, through overriding
the convertPropertyValue method. For example, encrypted values
can be detected and decrypted accordingly before processing them.
| Field Summary |
|---|
| public static final String | DEFAULT_BEAN_NAME_SEPARATOR | |
| Method from org.springframework.beans.factory.config.PropertyOverrideConfigurer Detail: |
protected void applyPropertyValue(ConfigurableListableBeanFactory factory,
String beanName,
String property,
String value) {
BeanDefinition bd = factory.getBeanDefinition(beanName);
while (bd.getOriginatingBeanDefinition() != null) {
bd = bd.getOriginatingBeanDefinition();
}
bd.getPropertyValues().addPropertyValue(property, value);
}
Apply the given property value to the corresponding bean. |
public boolean hasPropertyOverridesFor(String beanName) {
return this.beanNames.contains(beanName);
}
Were there overrides for this bean?
Only valid after processing has occurred at least once. |
protected void processKey(ConfigurableListableBeanFactory factory,
String key,
String value) throws BeansException {
int separatorIndex = key.indexOf(this.beanNameSeparator);
if (separatorIndex == -1) {
throw new BeanInitializationException("Invalid key '" + key +
"': expected 'beanName" + this.beanNameSeparator + "property'");
}
String beanName = key.substring(0, separatorIndex);
String beanProperty = key.substring(separatorIndex+1);
this.beanNames.add(beanName);
applyPropertyValue(factory, beanName, beanProperty, value);
if (logger.isDebugEnabled()) {
logger.debug("Property '" + key + "' set to value [" + value + "]");
}
}
Process the given key as 'beanName.property' entry. |
protected void processProperties(ConfigurableListableBeanFactory beanFactory,
Properties props) throws BeansException {
for (Enumeration names = props.propertyNames(); names.hasMoreElements();) {
String key = (String) names.nextElement();
try {
processKey(beanFactory, key, props.getProperty(key));
}
catch (BeansException ex) {
String msg = "Could not process key '" + key + "' in PropertyOverrideConfigurer";
if (!this.ignoreInvalidKeys) {
throw new BeanInitializationException(msg, ex);
}
if (logger.isDebugEnabled()) {
logger.debug(msg, ex);
}
}
}
}
|
public void setBeanNameSeparator(String beanNameSeparator) {
this.beanNameSeparator = beanNameSeparator;
}
Set the separator to expect between bean name and property path.
Default is a dot ("."). |
public void setIgnoreInvalidKeys(boolean ignoreInvalidKeys) {
this.ignoreInvalidKeys = ignoreInvalidKeys;
}
Set whether to ignore invalid keys. Default is "false".
If you ignore invalid keys, keys that do not follow the
'beanName.property' format will just be logged as warning.
This allows to have arbitrary other keys in a properties file. |