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.

[HTML]View PlainCopy
  1. Just add a line to the spring configuration file:<context:property-placeholder? location="classpath:jdbc.properties"/> The location value is the position of the parameter profile, and the parameter profile is usually placed in the SRC directory. The format of the parameter configuration file is the same as the Java common parameter configuration file, which is the form of key-value pairs, for example:
  2. #jdbc配置
  3. Test.jdbc.driverclassname=Com.mysql.jdbc.Driver
  4. Test.jdbc.url=jdbc:mysql://localhost:3306/test
  5. Test.jdbc.username=Root
  6. test.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:

<bean id= "Testdatasource" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name= "Driverclassname" value= "${test.jdbc.driverclassname}"/>
<property name= "url" value= "${test.jdbc.url}"/>
<property name= "username" value= "${test.jdbc.username}"/>
<property name= "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:

[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="UTF-8" ?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="Http://www.springframework.org/schema/context"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans /spring-beans-3.2.xsd
  7. Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.2.xsd ">
  8. <context:property-placeholder location="classpath*:conf/conf_a.properties"/>
  9. <Bean class="Com.xxx.aaa.Bean1"
  10. p:driverclassname="${modulea.jdbc.driverclassname}"
  11. p:url="${modulea.jdbc.url}"
  12. p:username="${modulea.jdbc.username}"
  13. p:password="${modulea.jdbc.password}"/>
  14. </Beans>
Conf/conf_a.properties:
[HTML]View PlainCopy
    1. Modulea.jdbc.driverclassname=Com.mysql.jdbc.Driver
    2. Modulea.jdbc.username=Cartan
    3. modulea.jdbc.password=Superman
    4. Modulea.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:
[HTML]View PlainCopy
  1. <? XML version= "1.0" encoding="UTF-8" ?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="Http://www.springframework.org/schema/context"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans /spring-beans-3.2.xsd
  7. Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.2.xsd ">
  8. <context:property-placeholder location="classpath*:conf/conf_b.properties"/>
  9. <Bean class="Com.xxx.bbb.Bean1"
  10. p:driverclassname="${moduleb.jdbc.driverclassname}"
  11. p:url="${moduleb.jdbc.url}"
  12. p:username="${moduleb.jdbc.username}"
  13. p:password="${moduleb.jdbc.password}"/>
  14. </Beans>
Conf/conf_b.properties:
[HTML]View PlainCopy
    1. Moduleb.jdbc.driverclassname=Com.mysql.jdbc.Driver
    2. Moduleb.jdbc.username=Cartan
    3. moduleb.jdbc.password=Superman
    4. Moduleb.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 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 removed

[HTML]View PlainCopy
    1. <context:property-placeholder location="classpath*:conf/conf_b.properties"/>

Then write an XML again:

[HTML]View PlainCopy
    1. <? XML version= "1.0" encoding="UTF-8" ?>
    2. <beans xmlns="Http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="Http://www.springframework.org/schema/context"
    5. xmlns:p="http://www.springframework.org/schema/p"
    6. xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans /spring-beans-3.2.xsd
    7. Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.2.xsd ">
    8. <context:property-placeholder location="classpath*:conf/conf*.properties"/>
    9. <import resource="A.xml"/>
    10. <import resource="B.xml"/>
    11. </Beans>

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.