Java Study Notes 6-use commons configuration to manage configuration files

Source: Internet
Author: User

It currently supports the following configuration file formats: Properties files XML documents Windows INI files Property list files (plist) jndi jdbc Datasource System properties Applet parameters Servlet parameters I am currently using the latest version 1.9, the following describes how to call a Properties file: 1. load the jar package. I use maven to automatically load it, pom. the xml configuration is as follows: [html] <dependency> <groupId> commons-configuration </groupId> <artifactId> commons-configuration </artifactId> <version> 1.9 </version> </dependency> <! -- Commons-configuration automatically loads version 2.1, and an error is reported during compilation, therefore, add the --> <dependency> <groupId> commons-lang </groupId> <artifactId> commons-lang </artifactId> <version> 2.6 </version> </dependency> The common-lang package must use the new version, if you do not write this dependency, commons-configuration will download an old 2.1 version, resulting in a compilation Error 2. java code: [java] PropertiesConfiguration config = new PropertiesConfiguration ("/database. properties "); String userName = config. getString ("name"); except getStrin In addition to the g () method, you can call methods of different return types, such as getBoolean, getDouble, and getInteger. Advanced usage: there are multiple configuration files in a project, so it is necessary to have a unified configuration file management class. I wrote a simple one for your reference, if you have any improper usage, please refer to 1. java class [java] package com. xxx. xxx. util; import java. util. hashMap; import java. util. map; import org. apache. commons. configuration. configurationException; import org. apache. commons. configuration. propertiesConfiguration;/***** <p> * read configuration file class * </p> * <p> * return attribute content based on the configuration file name and attribute key, configUtil. get (configFile, property); * </p> * @ author sheng Zhi. rensz **/public class configUtil {private static configUtil initor = new configUtil (); private static Map <String, Object> configMap = new HashMap <String, Object> (); private configUtil () {}/*** get content * @ param configFile * @ param property * @ return */public static String get (String configFile, String property) {if (! ConfigMap. containsKey (configFile) {initor. initConfig (configFile);} PropertiesConfiguration config = (PropertiesConfiguration) configMap. get (configFile); String value = config. getString (property); // todo log return value;}/*** load the configuration file. After initialization, add map * @ param configFile */private synchronized void initConfig (String configFile) {try {PropertiesConfiguration config = new PropertiesConfiguration (configFile); configMap. put (configFile, config);} catch (ConfigurationException e) {e. printStackTrace () ;}} 2. call method [java] configUtil. get ("/common/velocity. properties "," input. encoding ");

Related Article

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.