About the configuration class in Java

Source: Internet
Author: User

The properties file is the default configuration file format for the Java platform, with the advantages of a clear format, easy to understand, and simple to read the properties file with Commons-configuration code as follows:

Basic usage:

1. Load the jar package, I use MAVEN auto-load, the Pom.xml configuration is as follows:

[HTML]View PlainCopy
  1. <dependency>
  2. <groupId>commons-configuration</groupId>
  3. <artifactid>commons-configuration</artifactid>
  4. <version>1.9</version>
  5. </Dependency>
  6. <!--commons-configuration automatically loaded is 2.1 version, compile error, so add this--
  7. <dependency>
  8. <groupId>commons-lang</groupId>
  9. <artifactid>commons-lang</artifactid>
  10. <version>2.6</version>
  11. </Dependency>

Common-lang This package to use the new version, if you do not write this dependency, commons-configuration will download a 2.1 legacy, resulting in a compilation error

2.java Code:

[Java]View PlainCopy
    1. propertiesconfiguration config = new Propertiesconfiguration ("/database.properties");
    2. String userName = config.getstring ("name");

In addition to the GetString () method, methods that have different return types, such as Getboolean,getdouble,getinteger, can be called.

Advanced usage:

A project there will be more than one configuration file, then there is a unified profile management class is necessary, I wrote a simple, we can refer to, there is improper usage also please point out

1.java class

[Java]View PlainCopy
  1. Package com.xxx.xxx.util;
  2. Import Java.util.HashMap;
  3. Import Java.util.Map;
  4. Import org.apache.commons.configuration.ConfigurationException;
  5. Import org.apache.commons.configuration.PropertiesConfiguration;
  6. /**
  7. * <p>
  8. * Read configuration file class
  9. * </p>
  10. * <p>
  11. * Returns the property contents according to the profile name and the attribute key, Configutil.get (ConfigFile, property);
  12. * </p>
  13. * @author Shengzhi.rensz
  14. *
  15. */
  16. Public class Configutil {
  17. private static Configutil Initor = new Configutil ();
  18. private static map<string, object> Configmap = new hashmap<string, object> ();
  19. Private Configutil () {}
  20. /** 
  21. * Get content
  22. * @param configfile
  23. * @param Property
  24. * @return
  25. */
  26. public static string get (string ConfigFile, String property) {
  27. if (!configmap.containskey (configfile)) {
  28. Initor.initconfig (ConfigFile);
  29. }
  30. propertiesconfiguration config = (propertiesconfiguration) configmap.get (configfile);
  31. String value = config.getstring (property);
  32. //todo LOG
  33. return value;
  34. }
  35. /** 
  36. * Load configuration file and add map after initialization
  37. * @param configfile
  38. */
  39. private synchronized void Initconfig (String configfile) {
  40. try {
  41. propertiesconfiguration config = new propertiesconfiguration (configfile);
  42. Configmap.put (configfile, config);
  43. } catch (ConfigurationException e) {
  44. E.printstacktrace ();
  45. }
  46. }
  47. }


2. Calling methods

[Java]View PlainCopy
      1. Configutil.get ("/common/velocity.properties", "input.encoding");

[Java]View PlainCopy
    1. Public static void ReadProperties () throws ConfigurationException {
    2. Propertiesconfiguration pcfg = new Propertiesconfiguration ("config/cfg.properties");
    3. System.out.println (pcfg.getstring ("Platform.jre"));
    4. }


Note that the path here must be clear, the configuration file is placed in the Config folder;

About the configuration class in Java

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.