Details of the Configuration class in JAVA

  • 2020-12-20 03:35:18
  • OfStack

This article focuses on the use of the Configuration class in Java, involving maven auto-loading, ES4en.xml configuration, and simple Java code, as follows.

properties file is the default configuration file format of Java platform. Its advantage is that the format is clear and easy to understand. It is also simple to read properties file using commons-ES12en, with the code as follows:

Basic Usage:

1. Load the jar package. I used maven to load it automatically.


<dependency> 
  <groupId>commons-configuration</groupId> 
  <artifactId>commons-configuration</artifactId> 
  <version>1.9</version> 
</dependency> 
<!-- commons-configuration  The autoload is 2.1 Version of, error will be reported when compiled, so add this  --> 
<dependency> 
  <groupId>commons-lang</groupId> 
  <artifactId>commons-lang</artifactId> 
  <version>2.6</version> 
</dependency> 

The package es25EN-ES26en is to be used in the new version. If you do not write this dependency, ES27en-ES28en will download an old 2.1 version, causing a compilation error

2. java code:


PropertiesConfiguration config = new PropertiesConfiguration( " /database.properties " ); 
String userName = config.getString("name"); 

In addition to the getString() method, there are methods with different return types, such as getBoolean,getDouble,getInteger, that can be called.

Advanced usage:

1 project will have more than one configuration file, at this time there is a unified configuration file management class is very necessary, I wrote a simple, you can refer to the next, there is inappropriate use please also point out

1. java class


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 the configuration file class  
 * </p> 
 * <p> 
 *  According to the configuration file name and properties key Returns the property contents, 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() {
	}
	/** 
   *  Access to 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, initialize it and add it 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 the method


configUtil.get("/common/velocity.properties", "input.encoding"); 

public static void readProperties() throws ConfigurationException { 
    PropertiesConfiguration pcfg = new PropertiesConfiguration("config/cfg.properties"); 
    System.out.println(pcfg.getString("platform.jre")); 
  } 

Note that path 1 must make sure that the configuration file is in the config folder;

conclusion

This is the full text of the Configuration class in JAVA, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: