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
- <dependency>
- <groupId>commons-configuration</groupId>
- <artifactid>commons-configuration</artifactid>
- <version>1.9</version>
- </Dependency>
- <!--commons-configuration automatically loaded is 2.1 version, compile error, so add this--
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactid>commons-lang</artifactid>
- <version>2.6</version>
- </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
- propertiesconfiguration config = new Propertiesconfiguration ("/database.properties");
- 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
- 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>
- * Returns the property contents according to the profile name and the attribute key, Configutil.get (ConfigFile, property);
- * </p>
- * @author Shengzhi.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 configuration file and add map after initialization
- * @param configfile
- */
- private synchronized void Initconfig (String configfile) {
- try {
- propertiesconfiguration config = new propertiesconfiguration (configfile);
- Configmap.put (configfile, config);
- } catch (ConfigurationException e) {
- E.printstacktrace ();
- }
- }
- }
2. Calling methods
[Java]View PlainCopy
- Configutil.get ("/common/velocity.properties", "input.encoding");
[Java]View PlainCopy
- Public static void ReadProperties () throws ConfigurationException {
- Propertiesconfiguration pcfg = new Propertiesconfiguration ("config/cfg.properties");
- System.out.println (pcfg.getstring ("Platform.jre"));
- }
Note that the path here must be clear, the configuration file is placed in the Config folder;
About the configuration class in Java