I. knowledge points
In spring, propertyplaceholderconfigurer is the Bean Factory post-processor used to separately put some bean configurations into an attribute file. Variables in the form of $ {var} can be used in the bean configuration file. propertyplaceholderconfigurer loads attributes from the attribute file and replaces the variables with them.
The difference between the Bean Factory post-processor and the bean post-processor is that its objective is the IOC container --------- Bean factory or application context, rather than the bean instance. The Bean Factory post-processor takes effect after the bean configuration is loaded in the IOC container and before the bean instance is created. Its typical purpose is to modify the bean configuration before bean instantiation. Spring has multiple Bean Factory backprocessors.
Ii. Sample Code
Property file config. properties, placement path
cashier.path=c:/cashier
Bean Configuration
<! -- Register the Bean Factory post-processor propertyplaceholderconfigurer, configure an attribute file in the location attribute, and configure multiple attribute files for the locations attribute --> <Bean class = "org. springframework. beans. factory. config. propertyplaceholderpolicer "> <property name =" location "> <value> config. properties </value> </property> </bean> <bean id = "cashier1" class = "com. codeproject. jackie. springrecipesnote. springadvancedioc. cashier "init-method =" openfile "Destroy-method =" closefile "> <property name =" path "value =" $ {cashier. path} "/> </bean>
In spring2.5 and later versions, you can use<Context: Property-placeholder> elementRegister propertyplaceholderpolicer.
<? XML version = "1.0" encoding = "UTF-8"?> <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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/beans/spring-co Ntext-3.2.xsd "> <! -- Register the bean post-Processor --> <Bean class = "com. codeproject. Jackie. springrecipesnote. springadvancedioc. pathcheckingbeanpostprocessor"/> ........... <Context: Property-placeholder location = "config. properties "/> <bean id =" cashier1 "class =" com. codeproject. jackie. springrecipesnote. springadvancedioc. cashier "init-method =" openfile "Destroy-method =" closefile "> <property name =" path "value =" $ {cashier. path} "/> </bean> </beans>