You can use commons configuration to access configuration information stored in attribute files and XML documents. The dependent packages include commons language, commons collections, and commons logging.
1. Configure the application through the property File
The attribute file is used as the configuration file, which is easy to read, but the configuration content is not flexible.
For example, the configuration file config. properties is as follows:
No = 237
Names = kiyho, Sinkiang
Correct = false
You can use the following code to read data:
Import org. Apache. commons. configuration. configuration;
Import org. Apache. commons. configuration. configurationexception;
Import org. Apache. commons. configuration. propertiesconfiguration;
Public class propertiesconfigdemo {
Public static void main (string [] ARGs ){
Try {
Configuration Config = new propertiesconfiguration (
"Config. properties ");
System. Out. println ("score:" + config. getfloat ("score "));
System. Out. println ("names:" + config. getlist ("names "));
System. Out. println ("correct:" + config. getboolean ("correct "));
} Catch (configurationexception e ){
E. printstacktrace ();
}
}
}
I feel quite uncomfortable. I need to use a configuration and depend on the other three commons packages.
In addition, you can use the properties class of the util package to implement configuration reading, but the read data is of the string type and must be converted by yourself.
Import java. Io. ioexception;
Import java. util. properties;
Public class propertiesdemo {
Public static void main (string [] ARGs ){
Try {
Properties Properties = new properties ();
Properties. Load (thread. currentthread (). getcontextclassloader ()
. Getresourceasstream ("config. properties "));
System. Out. println ("score:" + properties. getproperty ("score "));
System. Out. println ("names:" + properties. getproperty ("names "));
System. Out. println ("correct:" + properties. getproperty ("correct "));
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}
2. Configure the application through XML
Assume that the following xml configuration file config. XML is available:
<? XML version = "1.0" encoding = "UTF-8"?>
<Config>
<Name> kiyho </Name>
<Name> Sinkiang </Name>
<Info>
<Age> 100 </age>
</INFO>
</Conifig>
You can use the following code to access:
Import org. Apache. commons. configuration. configurationexception;
Import org. Apache. commons. configuration. xmlconfiguration;
Public class xmlconfigdemo {
Public static void main (string [] ARGs ){
Try {
Xmlconfiguration Config = new xmlconfiguration ("config. xml ");
System. Out. println (config. getlist ("name "));
System. Out. println (config. getint ("info. Age "));
} Catch (configurationexception e ){
E. printstacktrace ();
}
}
}
In addition to configuation, you can also use JDOM to read xml configuration files:
Import java. Io. ioexception;
Import java. util. List;
Import org. JDOM. Document;
Import org. JDOM. element;
Import org. JDOM. jdomexception;
Import org. JDOM. Input. saxbuilder;
Public class jdomxmlconfigdemo {
Public static void main (string [] ARGs ){
Classloader = thread. currentthread ()
. Getcontextclassloader ();
Try {
Document document = new saxbuilder (). Build (classloader
. Getresourceasstream ("config. xml "));
List <element> names = Document. getrootelement (). getchildren ("name ");
String age = Document. getrootelement (). getchild ("info"). getchild (
"Age"). gettexttrim ();
For (element name: names)
System. Out. println ("name:" + name. gettexttrim ());
System. Out. println ("Age:" + age );
} Catch (jdomexception e ){
E. printstacktrace ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}
Does not rely on any commons package, as long as the JDOM package, but the read is string, you need to convert.
3. Composite Configuration
Multiple configuration files. Suppose there are three configuration files: Global. properties, local. properties, user. properties, where user takes precedence over local and local takes precedence over global. you can use the following three configuration files and one referenced configuration file to implement composite configuration.
The content of cofigs. XML is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Properties filename = "user. properties"/>
<Properties filename = "Local. properties"/>
<Properties filename = "Global. properties"/>
</Configuration>
The contents of Global. properties are as follows:
Name = default user
Age = 0
Speed = 10
The content of local. properties is as follows:
Name = kiyho
Age = 20
The content of user. properties is as follows:
Age = 100
The program reads the following:
Import org. Apache. commons. configuration. configuration;
Import org. Apache. commons. configuration. configurationexception;
Import org. Apache. commons. configuration. configurationfactory;
Public class configsdemo {
Public static void main (string [] ARGs ){
Configurationfactory factory = new configurationfactory ();
Factory. setconfigurationfilename ("configs. xml ");
Try {
Configuration Config = factory. getconfiguration ();
System. Out. println ("name:" + config. getstring ("name "));
System. Out. println ("Age:" + config. getint ("Age "));
System. Out. println ("Speed:" + config. getfloat ("Speed "));
} Catch (configurationexception e ){
E. printstacktrace ();
}
}
}
In addition, the packages on which configurationfactory depends are commons Digester and commons beanutils.
References: http://www.52z.com/info/html/28911.html