Problem
When configuring beans in the configuration file, you must remember that it’s not a good practice to mix deployment details, such as the file path, server address, username, and password, with your bean configurations. Usually, the bean configurations are written by application developers while the deployment details are matters for the deployers or system administrators.
Solution
Spring comes with a bean factory post processor called PropertyPlaceholderConfigurer for you to externalize part of the bean configurations into a properties file. You can use variables of the form ${var} in your bean configuration file and PropertyPlaceholderConfigurer will load the properties from a properties file and use them to replace the variables.
A bean factory post processor differs from a bean post processor in that its target is the IoC container—either the bean factory or the application context—not the bean instances. It will take effect on the IoC container after it loads the bean configurations but before any of the bean instances are created. The typical usage of a bean factory post processor is to alter the bean configurations before the beans are instantiated. Spring comes with several bean factory post processors for you to use. In practice, you seldom need to write your own bean factory post processors.
How It Works
Previously, you specified the logging path for a cashier in the bean configuration file. It is not a good practice to mix such deployment details with your bean configurations. A better approach is to extract the deployment details into a properties file, such as config.properties, in the root of the classpath. Then define the logging path in this file. cashier.path=c:/cashier Now you can use variables of the form ${var} in your bean configuration file. To load the external properties from a properties file and use them to replace the variables, you have to register the bean factory post processor PropertyPlaceholderConfigurer in your application context. You can specify either one properties file in the location property or multiple properties files in the locations property.
<beans …> … <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>config.properties</value> </property> </bean> <bean id="cashier1" class="com.shop.Cashier"> <property name="path" value="${cashier.path}" /> </bean> </beans>
Implemented as a bean factory post processor, PropertyPlaceholderConfigurer will replace the variables in your bean configuration file with the external properties before your beans get instantiated. In Spring 2.5, the registration of PropertyPlaceholderConfigurer can be simply through the <context:property-placeholder> element.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:property-placeholder location="config.properties" /> … </beans>