Java operation properties configuration file

Source: Internet
Author: User
Java has a class Properties (Java. util. properties), which is mainly used to read the Java configuration file and store some values that may need to be changed in properties for configuration. the properties file is actually a common text file. the file content is in the format of "key value". you can use & quot ;#& quot; to annotate text annotations. Try to use UTF-8 format for storage. The classes provided by jdk have disadvantages, so we usually use the commons-configuration framework for parsing. Java has a class Properties (Java. util. properties), which is mainly used to read the Java configuration file and store some values that may need to be changed in properties for configuration. the properties file is actually a common text file. the file content is in the format of "key = value". you can use "#" to annotate text annotations. Try to use UTF-8 format for storage. The classes provided by jdk have disadvantages, so we usually use the commons-configuration framework for parsing.

1.1.1. Properties class diagram

 
         
  
   commons-configuration
          commons-configuration        
  
   1.8
  
 



1.1.4.2. tool encapsulation


Package cn. xhgg. common. configuration; import java. util. hashMap; import java. util. iterator; import java. util. list; import java. util. map; import java. util. concurrent. concurrentHashMap; import org. apache. commons. configuration. configurationException; import org. apache. commons. configuration. propertiesConfiguration; import org. apache. commons. configuration. reloading. fileChangedReloadingStrategy; import org. apache. commons. lang. stringUtils; import org. slf4j. logger; import org. slf4j. loggerFactory;/*** for this type of configuration, the file requires a configuration file first, and the configuration file should be written to this class, the encoding method of the configuration file must be UTF8 */public class PropertiesConfigUtil {private static Logger log = LoggerFactory. getLogger (PropertiesConfigUtil. class); public static final String PROPS_SUFFIX = ". properties "; private static Map
 
  
ConfigMap = new ConcurrentHashMap
  
   
(); Private static PropertiesConfiguration getConfig (String configName) {// Remove space configName = configName. trim (); // use a suffix to add a suffix String configSig = StringUtils. endsWith (configName, PROPS_SUFFIX )? ConfigName: configName + PROPS_SUFFIX; if (configMap. containsKey (configSig) {return configMap. get (configSig);} PropertiesConfiguration config = null; try {config = new PropertiesConfiguration (); config. setEncoding ("UTF-8"); config. load (configSig); // Check config once every five seconds by default. setReloadingStrategy (new FileChangedReloadingStrategy (); config. setThrowExceptionOnMissing (true); configMap. put (configSig, config);} catch (ConfigurationException e) {e. printStackTrace ();} return config;} public static Map
   
    
GetKeyValuePairs (String configSig) {PropertiesConfiguration config = getConfig (configSig); if (config = null) {return null;} Iterator
    
     
Iters = config. getKeys (); Map
     
      
RetMap = new HashMap
      
        (); While (iters. hasNext () {String beforeKey = iters. next (); if (retMap. containsKey (beforeKey) {log. warn (configSig + "configKey:" + beforeKey + "repeated !! ");} RetMap. put (beforeKey, config. getString (beforeKey);} return retMap;}/*** method for obtaining parameters through PropertiesConfiguration *
       

** @ Return. */Static public String getString (String configSig, String key) {return getConfig (configSig ). getString (key);} static public String getString (String configSig, String key, String defaultValue) {return getConfig (configSig ). getString (key, defaultValue);} static public int getInt (String configSig, String key) {return getConfig (configSig ). getInt (key);} static public int getInt (String configSig, String key, int defaultValue) {return getConfig (configSig ). getInt (key, defaultValue);} static public boolean getBoolean (String configSig, String key) {return getConfig (configSig ). getBoolean (key);} static public boolean getBoolean (String configSig, String key, boolean defaultValue) {return getConfig (configSig ). getBoolean (key, defaultValue);} static public double getDouble (String configSig, String key) {return getConfig (configSig ). getDouble (key);} static public double getDouble (String configSig, String key, double defaultValue) {return getConfig (configSig ). getDouble (key, defaultValue);} static public float getFloat (String configSig, String key) {return getConfig (configSig ). getFloat (key);} static public float getFloat (String configSig, String key, float defaultValue) {return getConfig (configSig ). getFloat (key, defaultValue);} static public long getLong (String configSig, String key) {return getConfig (configSig ). getLong (key);} static public long getLong (String configSig, String key, long defaultValue) {return getConfig (configSig ). getLong (key, defaultValue);} static public short getShort (String configSig, String key) {return getConfig (configSig ). getShort (key);} static public short getShort (String configSig, String key, short defaultValue) {return getConfig (configSig ). getShort (key, defaultValue);} static public ListGetList (String configSig, String key) {return getConfig (configSig). getList (key);} static public ListGetList (String configSig, String key, ListDefaultValue) {return getConfig (configSig ). getList (key, defaultValue);} static public byte getByte (String configSig, String key) {return getConfig (configSig ). getByte (key);} static public byte getByte (String configSig, String key, byte defaultValue) {return getConfig (configSig ). getByte (key, defaultValue);} static public String [] getStringArray (String configSig, String key) {return getConfig (configSig ). getStringArray (key );}}



1.1.4.3. introduce the properties Test File

The configuration of rabbitmq. properties is as follows:


# Rpc mode rmqrpc. rabbit. host = l-opsdev3.ops.bj0.jd.comrpc.rabbit.port = 5672rpc. rabbit. username = jd_vrmphotorpc.rabbit.password = signature = jd/vrmphotorpc. rabbit. queue = signature = photoworkerrpc. rabbit. key = photoworker.



1.1.4.4. test


PropertiesConfigUtil config=new PropertiesConfigUtil();String username = config.getString("rabbitmq", "rpc.rabbit.username");System.out.println(username);



Output result:

Jd_vrmphoto

OK.

1.1.4.5. considerations

1. the best encoding UTF-8 unified.

ReloadingStrategy policy selection. You can see the specific implementation classes and use cases. I usually use the FileChangedReloadingStrategy class.

The above is the content of the properties configuration file for java operations. For more information, see PHP Chinese network (www.php1.cn )!

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.