Apache Commons Configuration introduction and simple code
Apache Commons Configuration The latest version is 2.0alpha, for security purposes, we use the 1.1 official version, the main function is to read the resource files, each file format has a corresponding class, as follows:
· Properties Files
· XML Documents
· Windows INI files
· Property List Files (plist)
· JNDI
· JDBC Datasource
· System Properties
· Applet Parameters
· Servlet Parameters
The use of configuration can be relatively simple to get some information, a simple source code example:
Packagetest.ffm83.commons.configuration;
Importorg.apache.commons.configuration.CompositeConfiguration;
Importorg.apache.commons.configuration.ConfigurationException;
Importorg.apache.commons.configuration.PropertiesConfiguration;
/**
* Simple configuration via commons configurations
*
* @author Fan Fangming
*/
Public Classconfigurationusage {
public static void Main (string[] args) {
String Jdbcurl =propmanager.getinstance (). GetProperty ("Jdbc.cms.url");
String jdbcusername =propmanager.getinstance (). GetProperty (
"Jdbc.cms.username");
String Jdbcpassword =propmanager.getinstance (). GetProperty (
"Jdbc.cms.password");
System.out.println ("Jdbcurl:" + jdbcurl);
System.out.println ("Jdbcusername:" + jdbcusername);
System.out.println ("Jdbcpassword:" + Jdbcpassword);
}
Private Configurationusage () {
}
private static Configurationusagepropmanager;
Public synchronized Staticconfigurationusage getinstance () {
if (Propmanager = = null) {
Propmanager = Newconfigurationusage ();
}
return propmanager;
}
public static compositeconfigurationconfig = new Compositeconfiguration ();
static {
try {
Config.addconfiguration (Newpropertiesconfiguration (
"Ffm.properties"));
} catch (ConfigurationException e) {
E.printstacktrace ();
}
}
public string GetProperty (string key) {
return config.getstring (key);
}
}
The results of the operation are as follows:
Jdbcurl:root
Jdbcusername:fanfangming
Jdbcpassword:ffm
The contents of the "ffm.properties" configuration file are as follows:
Jdbc.cms.url=root
Jdbc.cms.username=fanfangming
Jdbc.cms.password=ffm
Apache Commons configuration file processing for XML is also very quick and easy, the following is the source code to read XML with the configuration
Package test.ffm83.commons.configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
/**
* through Commons Configuration for a simple XML Configuration
* XML can be in Chinese, or in English, but not full-width characters
* @author Fan Fangming
*/
Public class Configurationxmlusage {
Public static void main (string[] args) {
Stringfile = "Ffm.xml";
Try {
Xmlconfigurationconfig = newxmlconfiguration (file);
System. out. println (" config file "+ file+" load succeeded ");
System. out. println (" company Name:" + config.getstring ( " company name") "));
System. out. println (" person Name:" + config.getstring (" application configuration parameter . Name of person "));
System. out. println (" worker Number:" + config.getstring (" application configuration parameters . Workno "));
System. out. println (" Office Hours:" + config.getstring (" application configuration Parameters . Attendance Time . Working Hours "));
}catch(Configurationexceptione) {
E.printstacktrace ();
}
}
}
The operation results are as follows
Configuration file Ffm.xml loaded successfully
Company Name: China Telecom
Person Name: Fan Fangming
Worker Number: 31000696
Working hours: 08:30:00
The configuration file Ffm.xml content is as follows:
<? XML version="1.0"encoding="Utf-8"?>
< project Configuration >
< Company Name > China Telecom </ company name >
< Application Configuration Parameters >
< Name of person > Fan Fangming </ person name >
< Workno >31000696</workno>
< Attendance Time >
< Working Hours >08:30:00</ Work hours >
< Time off >17:00:00</ time off >
< late miners time dividing point >08:45:00</ late miners time demarcation point >
</ Attendance Time >
</ Application Configuration Parameters >
</ project Configuration >
Apache Commons's Commons-configuration