PropertyPlaceHolderConfigurer A property resource configurer that resolves placeholders in bean property values of context definitions. It pulls values from a properties file into bean definitions.
The default placeholder syntax follows the Ant / Log4J / JSP EL style: ‘$’
PropertyOverrideConfigurer A property resource configurer that overrides bean property values in an application context definition. It pushes values from a properties file into bean definitions
Example:
A simple class of which values are set from property file.
public class SpringPropertiesTest implements SpringPropInterface
{
private String configValue;
private String overrideValue;public SpringPropertiesTest(){}public String getConfigValue()
{
return this.configValue;
}
public String getOverrideValue()
{
return this.overrideValue;
}public void setOverrideValue(final String overrideValue)
{
this.overrideValue = overrideValue;
}
}// The interface
public interface SpringPropInterface
{
String getConfigValue();String getOverrideValue();void setConfigValue(final String configValue);void setOverrideValue(final String overrideValue);
}// the main class to access properties
public class SpringPropertiesMain
{
public static void main(final String arguments[])
{
final ConfigurableApplicationContext context =
new ClassPathXmlApplicationContext("/spring-properties-example.xml");
final SpringPropInterface testClass = (SpringPropertiesTest)
context.getBean("SpringPropertiesHandlingBean");System.err.println( "Configurer Value: "+ testClass.getConfigValue());
System.err.println( "Overrider Value: " + testClass.getOverrideValue());
}
}the spring-properties-example.xml .< bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer"
p:locations="classpath:/spring-properties-override.properties" / >
< bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:locations="classpath:/spring-properties-configure.properties" />
< bean id="SpringPropertiesHandlingBean"
class="SpringPropertiesTest"
p:configValue="${value.configurer}"
p:overrideValue="yadayadayada" / >
spring-properties-configure.properties
value.configurer=Hello, World!
value.overrider=Hello, Spring!
spring-properties-override.properties
SpringPropertiesHandlingBean.overrideValue=Overridden! The first properties file is used to set the two values of the simple Java class and the second properties files overrides the value of the second of the two values of that Java class.
The output from running this looks like this:
Configurer Value: Hello, World!Overrider Value: Overridden!
These two pieces of output show that the PropertyPlaceholderConfigurer configures the values in the Spring application based on properties files and the PropertyOverrideConfigurer allows these values to be overridden. Of course, one could override hard-coded values in the XML as well, but it was interesting to see both the original values set with PropertyPlaceholderConfigurer and then see them overridden with PropertyOverrideConfigurer
Filed under: spring