How spring uses the Propertyconfigurer class to read the. Property Database configuration file

Source: Internet
Author: User
Tags config

1.Spring frame, the Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer class can have the. Properties (Key/value form ) file

Some dynamically set values (value), which are replaced with the value of the placeholder ($key $) in the XML.

The. properties file can customize some of the relevant parameters based on customer needs, and this design provides flexibility for the program.

2. In spring, you can use Propertyplaceholderconfigurer to add an external property file to an XML configuration file and, of course, to specify the encoding of an external file, such as:

<bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >  
    <property name= "Location" >  
      <value>conf/sqlmap/jdbc.properties</value>  
    </ property>  
     <property name= "fileencoding" >  
       <value>UTF-8</value>  
     </property >  
</bean>

Of course, you can also introduce multiple property files, such as:

<bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >  
   <property name= "Locations" >  
    <list>  
     <value>/web-inf/mail.properties</value >     
     <value>classpath:conf/sqlmap/jdbc.properties</value>//pay attention to the two kinds of value of the notation  
    </list>  
   </property>  
</bean>

The basic use of this method is:

XML code

<bean id= "Propertyconfigurerforanalysis" class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ">  
    <property name=" Location " >  
        <value>classpath:/spring/include/dbQuery.properties</value>  
    </property>  
    <property name= "fileencoding" >  
       <value>UTF-8</value>  
     </property>  
</ Bean>

The classpath is a reference to the SRC directory of the file.

When multiple properties files exist, the configuration will need to use locations:

XML code

<bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >  
    <property name= "Locations" >  
       <list>  
          <value>classpath:/spring/include/ Jdbc-parms.properties</value>  
          <value>classpath:/spring/include/base-config.properties</ value>  
          <value>classpath*:config/jdbc.properties</value>  
        </list>  
    </property >  
</bean>

Next we need to use multiple propertyplaceholderconfigurer to distribute the configuration, to achieve the integration of multiple projects under a number of decentralized properties files, which are configured as follows

XML code

<bean id= "PropertyConfigurerForProject1" class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ">  
    <property name=" Order "value= "1"  />  
    <property name= "Ignoreunresolvableplaceholders" value= "true"  />  
    <property Name = "Location" >  
       <value>classpath:/spring/include/dbQuery.properties</value>  
    </property >  
</bean>

XML code

<bean id= "PropertyConfigurerForProject2" class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ">  
    <property name=" Order "value= "2"  />  
    <property name= "Ignoreunresolvableplaceholders" value= "true"  />  
    <property Name = "Locations" >  
      <list>  
        <value>classpath:/spring/include/jdbc-parms.properties</value >  
        <value>classpath:/spring/include/base-config.properties</value>  
      </list>  
    </property>  
</bean>

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

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.