The spring framework provides you with a Beanfactorypostprocessor implementation category: Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer. With this category, you can move some of the configuration settings out to the. properties file, so that the XML definition file is responsible for system-related settings, and the. Properties document can be used as a customer to customize some relevant parameters as required.
Take a look at the actual example of a bean definition file:
Beans-config.xml
XML code:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Beans Public "-//spring/dtd bean/en" "Http://www.springframework.org/dtd/spring-beans.dtd "><Beans><BeanID= "Configbean"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
< Propertyname= "Location">
<value>Hello.properties</value>
</ Property>
</Bean><BeanID= "Hellobean"class= "Onlyfun.caterpillar.HelloBean">< Propertyname= "Helloword"><value>${onlyfun.caterpillar.helloword}</value></ Property></Bean></Beans>
It is assumed that there are many properties of dependency injection in Hellobean, which are relatively infrequently variable, and where Helloword changes frequently, Can be easily set through hello.properties, and this information is set in the Configbean location property:
Hello.properties
onlyfun.caterpillar.helloword=welcome!
In the Helloword attribute of Hellobean, ${onlyfun.caterpillar.helloword} will be replaced by Helloword of Hello.properties.
If you have more than one. properties file, you can set it through the locations property, for example:
Beans-config.xml
XML code:
<?XML version= "1.0" encoding= "UTF-8"?> <!DOCTYPE Beans Public "-//spring/dtd bean/en" "Http://www.springframework.org/dtd/spring-beans.dtd "> <Beans> <BeanID= "Configbean"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> < Propertyname= "Locations"> <List> <value>Hello.properties</value> <value>Welcome.properties</value> <value>Other.properties</value> </List> </ Property> </Bean> <BeanID= "Hellobean"class= "Onlyfun.caterpillar.HelloBean"> < Propertyname= "Helloword"> <value>${onlyfun.caterpillar.helloword}</value> </ Property> ... </Bean> </Beans>
Propertyplaceholderconfigurer class is a kind of bean factory post-processor, its function is a resource property of the Configurator, The ability to place content defined in the beanfactory in a file with the. propertis suffix.
such as---spring-context.xml----
XML code:
<BeanID= "Propertyconfigurer"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> < Propertyname= "Locations"> <List> <value>/web-inf/jdbc.properties</value> </List> </ Property></bean>
</Bean> <BeanID= "DataSource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource"> < Propertyname= "Driverclassname"><value>${driver}</value></ Property> < Propertyname= "url"><value>Jdbc:${dbname}</value></ Property> </Bean>
and the actual Jdbc.propertis file is
Jdbc.driverclassname=org.hsqldb.jdbcdriver jdbc.url=jdbc:hsqldb:hsql://production:9002 Jdbc.username=sa Jdbc.password=root
And how Jdbc.propertis is quoted: Registering the upper section of the configuration in Web. XML is possible
XML code:
<Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>/web-inf/spring-context.xml</Param-value> </Context-param>of course, don't forget spring's listener registration<Listener> <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class> </Listener>
In this way, a simple data source is set up.
In fact, the role of Propertyplaceholderconfigurer is to place the database configuration information pointed to by the placeholder in the bean definition
The tool.
About Propertyplaceholderconfigurer in spring