Context:property-placeholder

Source: Internet
Author: User

This is very common in the spring configuration file.

Context:property-placeholder greatly facilitates the configuration of our database.

Just add a line to the spring configuration file:<context:property-placeholder?location= "Classpath: Jdbc.properties "/>?" Here is the location value of the parameter configuration file, the parameter profile is usually placed in the SRC directory, and the parameter configuration file is the same as the Java generic parameter configuration file. The form of a key-value pair, for example: #jdbc配置test. jdbc.driverclassname=com.mysql.jdbc.drivertest.jdbc.url=jdbc:mysql://localhost:3306/ Testtest.jdbc.username=roottest.jdbc.password=root

This allows you to set values for the properties of the bean that is configured for spring, such as spring has a JDBC data source class Drivermanagerdatasource

This defines the bean in the configuration file:

<BeanID= "Testdatasource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource">    < Propertyname= "Driverclassname"value= "${test.jdbc.driverclassname}"/>    < Propertyname= "url"value= "${test.jdbc.url}"/>    < Propertyname= "username"value= "${test.jdbc.username}"/>    < Propertyname= "Password"value= "${test.jdbc.password}"/></Bean>

This modification is also convenient, but also unified this specification.

It is also important to note that if you encounter the following problem:

Both A and B modules have their own spring XML configuration, each with its own configuration file:

The spring configuration file for the A module is as follows:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xmlns:p= "http://www.springframework.org/schema/p"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/context /spring-context-3.2.xsd ">   <Context:property-placeholder Location= "Classpath*:conf/conf_a.properties"/>   <Beanclass= "Com.xxx.aaa.Bean1"P:driverclassname= "${modulea.jdbc.driverclassname}"P:url= "${modulea.jdbc.url}"P:username= "${modulea.jdbc.username}"P:password= "${modulea.jdbc.password}"/></Beans>

Conf/conf_a.properties:

Modulea.jdbc.driverclassname=com.mysql.jdbc.Drivermodulea.jdbc.username=  Cartanmodulea.jdbc.password=supermanmodulea.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea? Useunicode=true&characterencoding=utf8

The spring configuration file for the B module is as follows:

    <?XML version= "1.0" encoding= "UTF-8"?>      <Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xmlns:p= "http://www.springframework.org/schema/p"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/conte Xt/spring-context-3.2.xsd ">         <Context:property-placeholder Location= "Classpath*:conf/conf_b.properties"/>         <Beanclass= "Com.xxx.bbb.Bean1"P:driverclassname= "${moduleb.jdbc.driverclassname}"P:url= "${moduleb.jdbc.url}"P:username= "${moduleb.jdbc.username}"P:password= "${moduleb.jdbc.password}"/>      </Beans>

Conf/conf_b.properties:

Moduleb.jdbc.driverclassname=com.mysql.jdbc.Drivermoduleb.jdbc.username=  Cartanmoduleb.jdbc.password=supermanmoduleb.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea? Useunicode=true&characterencoding=utf8

It is normal to run a module alone or run the B module separately, but after integrating A and B two modules, the spring container will not start:
Could not resolve placeholder ' moduleb.jdbc.driverClassName ' in string value "${moduleb.jdbc.driverclassname}"
Cause: The spring container uses the discovery mechanism of reflection scanning, A bean that detects a org.springframework.beans.factory.config.PropertyPlaceholderConfigurer in the spring container stops the remaining propertyplaceholderconf Scanning of the igurer.

Only one context:property-placeholder can be defined in the spring container, or there will be such a mistake, how to solve the above problem?

A and B modules are removed.

<location= "Classpath*:conf/conf_b.properties"/>

Then write an XML again:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xmlns:p= "http://www.springframework.org/schema/p"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/context /spring-context-3.2.xsd ">   <Context:property-placeholder Location= "Classpath*:conf/conf*.properties"/>   <ImportResource= "A.xml"/>   <ImportResource= "B.xml"/></Beans>

zhuanzi:http://blog.csdn.net/sunhuwh/article/details/15813103

Context:property-placeholder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.