Spring configuration loads multiple properties files

Source: Internet
Author: User

A
First, we'll start with the spring configuration file. Defines a class that specifically reads the properties file.
Cases:

1 class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >2     < Property Name= "Locations" >3     <list>4         <value>classpath*:jdbc.properties </value>5         <!--If you have more than one profile, just continue adding it here--6     </list>7     </property>8 </bean>        



Why use locations here (there is also a location)
In general, there may be more than one configuration file in our project.
Even if there is only one, the new additions in the future, just add a value tag below,

Without having to re-modify too much.

(ii) Use of Context:property-placeholder in spring
Cases:

<context:property-placeholder location= "Classpath:data/mybatis.properties"/>

However, multiple properties files cannot be loaded, and the spring container uses a discovery mechanism for reflection scanning, in which a spring container is detected Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer's The bean will stop scanning the remaining propertyplaceholderconfigurer (Spring 3.1 has used propertysourcesplaceholderconfigurer instead Propertyplaceholderconfigurer).

In other words, the spring container allows only a maximum of one propertyplaceholderconfigurer (or) to be defined, and the rest will be ignored by spring (in fact, if spring provides a warning).

For example, if the A and B modules are running separately, because the spring container has only one propertyplaceholderconfigurer, the properties file will be loaded and replaced normally. If the A and B two modules are integrated and run, there are two propertyplaceholderconfigurer beans in the spring container, and then look who first who is behind, the first reservation, after the neglect! As a result, only one property file is loaded, resulting in an issue where property substitution cannot be performed correctly.

Workaround:

1     <!--load all configuration Files--2     <context:property-placeholder location= "classpath*:d ata/*.properties"/ >3     <!--can also be loaded in multiple directories--4     




Spring configuration loads multiple properties files

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.