Spring propertyplaceholderconfigurer Custom Extension

Source: Internet
Author: User
Tags config


Reference: http://seraph115.iteye.com/blog/435165

Spring Propertyplaceholderconfigurer This class, which is used to parse the Java Properties property file value and provides a substitute for using the property value during the spring configuration. Let's step into its configuration.

The basic way to use this is:

<bean id= "propertyconfigurerforanalysis" 
      class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer ">  
    <property name=" Location " >  
        <value>classpath:/spring/include/dbQuery.properties</value>  
    </property>  

Where Classpath is a reference to the file notation under the SRC directory.

When there are multiple properties files, the configuration requires the use of locations: (2)

<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>  
        </list>  
    </property >  
</bean>  

Next we will use multiple propertyplaceholderconfigurer to disperse the configuration, to achieve the integration of multiple projects under the decentralized properties file, the configuration is as follows: (3)

<bean id= "PropertyConfigurerForProject1" class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> <property name=" Order "value=" 1 "/&  
    Gt <property name= "Ignoreunresolvableplaceholders" value= "true"/> <property name= "Locations" > <l ist> <value>classpath:/spring/include/dbQuery.properties</value> </list> &L t;/property> </bean> <bean id= "PropertyConfigurerForProject2" class= "org.springframework.beans.f Actory.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>
 

Where the Order property represents the sequence in which it is loaded, if it is not set according to the order in which the XML file is loaded, and ignoreunresolvableplaceholders to whether the unresolved placeholder is ignored, If more than one propertyplaceholderconfigurer is configured, the property must be set and true, otherwise PropertyConfigurerForProject2 's properties file will not be loaded .

Now that you've learned how to use propertyplaceholderconfigurer, how to use multiple properties files, And how to configure multiple Propertyplaceholderconfigurer to decompose the scattered properties files in the project. As for Propertyplaceholderconfigurer there are more extension applications, such as the property file encryption and decryption methods will be in the following blog post continue to write.

Precautions:
(1) If the above dbquery.properties and jdbc-parms.properties file has the same parameter configuration name, the parameter value configured in Dbquery.properties will not be overwritten by the following;
(2) If the jdbc-parms.properties,base-config.properties is configured with the same parameter name as each other, the configured values in the jdbc-parms.properties will be overwritten;

Custom extension Propertyplaceholderconfigurer Implementation

For example: The path of the configuration file, need to determine dynamically, you need to extend the implementation of the propertyplaceholderconfigurer itself, get the file path, load properties file, Then add the properties after load to Propertyplaceholderconfigurer

Package com.common.spring.ext;
Import java.util.Properties;

Import Java.util.Set;

Import Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
Import com.common.exception.ApplicationException;
Import com.common.util.GlobalProperties;


Import Com.common.util.PropertiesUtil; public class Gollfpropertyplaceholderconfigurer extends Propertyplaceholderconfigurer {public void setgollfpropfiles (set<string> gollfpropfiles) {String Proppath = Globalproperties.getproperty (Globalproperties.properties_folder_path);//Get path by other configuration St

        Ring Fileseparator = System.getproperty ("File.separator");
        Properties Properties = new properties (); for (string gollfpropfile:gollfpropfiles) {string nodeName = System.getproperty ("WebLogic.
            Name "); Gollfpropfile = Gollfpropfile.replaceall ("\\[node_name\\]", nodeName);

     Node_name is a String file = Proppath + Fileseparator + gollfpropfile based on different WebLogic servers;       try {logger.info ("Loading properites file from" + file); Properties prop = propertiesutil.loadproperties (file);
                Return to properties File Logger.debug ("Properties" + prop);
                if (prop! = null) {Properties.putall (prop); }} catch (Exception e) {logger.fatal (New ApplicationException ("Properties file" + Gollfprop File + "cannot be found.
            All related functionalities is unavailable ", E, true)); }} this.setproperties (properties);

 The key method, called the method in the Propertyplaceholderconfigurer,//Through this method to add the custom loaded properties file into spring}}

XML configuration

<bean id= "auditjmsproperties" 
       class= "Com.common.spring.ext.GollfPropertyPlaceholderConfigurer" >
        <property name= "Gollfpropfiles" >
            <set>
                <value>[node_name]_jms.properties</value >
            </set>
        </property>
    </bean>


When the properties file is loaded in Propertyplaceholderconfigurer, the actual call is: O The mergeproperties in Rg.springframework.core.io.support.PropertiesLoaderSupport

Spring Source

Protected Properties Mergeproperties () throws IOException {
  Properties result = new Properties ();

  if (this.localoverride) {
   //Load properties from the file upfront, to-let local properties override.
   LoadProperties (result);
  }

  if (this.localproperties! = null) {
   for (properties localProp:this.localProperties) {
       //) The user-defined property value is loaded, Merge
       Collectionutils.mergepropertiesintomap (Localprop, result) loaded with spring;}
  }

  if (!this.localoverride) {
   //Load properties from the file afterwards, to-let those properties override.
   LoadProperties (result);
  }

  return result;
 }



Loads the configuration from multiple properties files and then merges them back into a single Properties object.
The This.setproperties (properties) method above is to set the Localproperties reference, Localproperties is not empty, Merges user-defined properties into the Result Property object loaded by spring
Localoverride parameter: True, indicates that the user custom loaded property value overrides the spring system loaded if the name is the same.

Custom Usage Note : The invocation of a user-defined method must be called before spring initializes the call to the Mergeproperties () method of Propertyplaceholderconfigurer. Otherwise, the configuration file will not be merged. Called when the set value is normal.

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.