JAVA Configuration class details, javaconfiguration
This article focuses on the usage of the Configuration class in Java, involving automatic maven loading, pom. xml Configuration, and simple Java code, as shown below.
The properties file is the default configuration file format of the Java platform. Its advantage is that the format is clear and easy to understand. It is also easy to read the properties file using commons-configuration. The Code is as follows:
Basic usage:
1. Load the jar package. I use maven to automatically load it. The pom. xml configuration is as follows:
<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 this dependency is not written, commons-configuration downloads the old version 2.1, causing compilation errors.
2. java code:
PropertiesConfiguration config = new PropertiesConfiguration(“/database.properties”); String userName = config.getString("name");
Besides the getString () 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 point it 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 configuration file class * </p> * <p> * return attribute content based on the configuration file name and 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 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
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 the path here must be clear, and the configuration file is placed in the config folder;
Summary
The above is all the details about the Configuration class in JAVA, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!