Jakarta commons configuration Packet read configuration file
There are two common types of profiles: key-value pairs, or XML configuration files, which can be read with commons configuration packages.
The key-value pair format is the common. Properties file. Read through Propertiesconfiguration, as follows:
The code is as follows |
Copy Code |
Package com.guoweiwei.test.configuration; Import java.util.List; Import org.apache.commons.configuration.Configuration; Import org.apache.commons.configuration.ConfigurationException; Import org.apache.commons.configuration.PropertiesConfiguration; public class Propertiesreadexample { /** * @param args * @throws ConfigurationException */ public static void Main (string[] args) throws ConfigurationException { Configuration config = new Propertiesconfiguration ("Com/guoweiwei/test/configuration/test.properties"); Float speed = config.getfloat ("Speed"); List names = config.getlist ("names"); Boolean correct = Config.getboolean ("correct"); System.out.println ("Speed:" + speed); System.out.println ("names:" + names.size ()); System.out.println ("correct:" + correct); } } |
Of course, the reading and parsing of XML files is as simple as this:
The code is as follows |
Copy Code |
Package Com.guoweiwei.test.configuration;import java.util.List; Import org.apache.commons.configuration.Configuration; Import org.apache.commons.configuration.ConfigurationException; Import org.apache.commons.configuration.XMLConfiguration; public class Xmlreadexample { private static final String Resource_file = "Com/guoweiwei/test/configuration/test.xml"; /** * @param args * @throws ConfigurationException */ public static void Main (string[] args) throws ConfigurationException { Configuration config = new xmlconfiguration (resource_file); List Startcriteria = config.getlist ("Start-criteria.criteria"); String Firstcriteria = config.getstring ("Start-criteria.criteria (0)"); String Firstcriteriatype = config.getstring ("start-criteria.criteria (0) [@type]"); int horsepower = Config.getint ("horsepower"); System.out.println ("Startcriteria:" + startcriteria.size ()); System.out.println ("Firstcriteria:" + Firstcriteria); System.out.println ("Firstcriteriatype:" + firstcriteriatype); System.out.println ("horsepower:" + horsepower); } } |
As you can see from the code above, the Xmlconfiguration getxxx () method also supports XPath parsing of XML files.
In addition, you can use XML and property files to read a configuration file in a comprehensive way, such as a common scenario:
The configuration of the application has several different locations. The first is a global configuration, and then a local configuration may be required, when the two are repeated in a local configuration, then there is a user-defined profile, and the user-defined profile priority is higher than the local configuration and global configuration. This time you can consider using a configurationfactory to manage, the three different configuration files are defined as Global.properties, local.properties, User.properties, and then manages the priority relationships among the three through a configuration.xml file, as follows:
One of the property configuration files Global.properties
threads.max=50threas.min=2
timout=15.52
Interactive=true
Color=red
Speed=50
Name=default User
Property configuration File bis Local.properties
Threads.max=30speed=55
Three user.properties of the property configuration file
Threads.min=1color=black
speed=5000
Name=sean
Manage configuration files for these three property profiles Configuration.xml
The code is as follows |
Copy Code |
<?xml version= "1.0″encoding=" utf-8″?><configuration> <properties filename= "User.properties"/> <properties filename= "Local.properties"/> <properties filename= "Global.properties"/> </configuration>
|
You can then use this in your application:
The code is as follows |
Copy Code |
Package Com.guoweiwei.test.configuration;import java.net.MalformedURLException; Import Java.net.URL; Import org.apache.commons.configuration.Configuration; Import org.apache.commons.configuration.ConfigurationException; Import Org.apache.commons.configuration.ConfigurationFactory; public class Complexconfigexample { private static final String Resource_file = "Com/guoweiwei/test/configuration/complex/configuration.xml"; /** * @param args * @throws ConfigurationException * @throws malformedurlexception */ public static void Main (string[] args) throws ConfigurationException, Malformedurlexception { Complexconfigexample C = new Complexconfigexample (); C.readcomplexconfiguration (); } private void Readcomplexconfiguration () throws configurationexception{ Configurationfactory factory = new Configurationfactory (); URL url = this.getclass (). getresource (Resource_file); URL url = this.getclass (). GetResource ("Complex/configuration.xml"); Factory.setconfigurationurl (URL); Configuration config = factory.getconfiguration (); System.out.println ("Timeout:" +config.getfloat ("Timout")); System.out.println ("Max Thread:" +config.getint ("Threads.max")); System.out.println ("Name:" +config.getstring ("name")); System.out.println ("Speed:" +config.getint ("Speed")); } } |
The final output results are as follows:
timeout:15.52 "from Global.properties" Max thread:30 "from Local.properties"
Name:sean "from User.properties"
speed:5000 "from User.properties"
Spring reads a configuration file
I. Reading an XML configuration file
(a) Create a new Java bean (Hellobean.java)
Java code
The code is as follows |
Copy Code |
Package chb.demo.vo;
public class Hellobean { Private String HelloWorld;
Public String Gethelloworld () { return helloWorld; }
public void Sethelloworld (String helloWorld) { This.helloworld = HelloWorld; } }
|
(ii) Construction of a configuration file (Beanconfig.xml)
XML code
code is as follows |
copy code |
<!---->xml version= "1.0" encoding= "UTF-8"?> <!---->> <beans> <bean id= "Hellobean" class= "Chb.demo.vo.HelloBean" > <property name= "HelloWorld" > <value>Hello!chb!value> property> bean> beans> |
(iii) Reading XML files
1. Using Classpathxmlapplicationcontext
Java code
The code is as follows |
Copy Code |
ApplicationContext context = new Classpathxmlapplicationcontext ("Beanconfig.xml"); Hellobean Hellobean = (Hellobean) context.getbean ("Hellobean"); System.out.println (Hellobean.gethelloworld ()); |
2. Using Filesystemresource to read
Java code
The code is as follows |
Copy Code |
Resource rs = new Filesystemresource ("D:/software/tomcat/webapps/springwebdemo/web-inf/classes/beanconfig.xml"); Beanfactory factory = new Xmlbeanfactory (RS); Hellobean Hellobean = (Hellobean) factory.getbean ("Hellobean"); System.out.println (Hellobean.gethelloworld ()); |
It is worth noting that, with Filesystemresource, the configuration file must be placed in the project direct directory, or the absolute path should be specified, or it will throw an exception that cannot find the file
Two. Read properties configuration file
Here are two techniques: using spring to read properties files and using Java.util.Properties to read
(i) using spring to read properties files
We also use the Hellobean.java file above to construct the following beanconfig.properties file:
Properties Code
The code is as follows |
Copy Code |
Hellobean.class=chb.demo.vo.hellobean hellobean.helloworld=hello!chb! |
The "Hellobean" name in the property file is the alias setting for the Bean. Class is used to specify the source of the class.
Then use Org.springframework.beans.factory.support.PropertiesBeanDefinitionReader to read the property file
Java code
code is as follows |
copy code |
Beandefinitionregistry reg = new Defaultlistablebeanfactory (); Propertiesbeandefinitionreader reader = new Propertiesbeandefinitionreader (reg); reader.loadbeandefinitions (New Classpathresource (" Beanconfig.properties ")); beanfactory factory = ( beanfactory) reg; Hellobean Hellobean = (Hellobean) Factory.getbean ("Hellobean"); System.out.println (Hellobean.gethelloworld ()); |
(ii) using Java.util.Properties to read property files
For example, we construct a ipconfig.properties to hold the server IP address and port, such as:
Properties Code
ip=192.168.0.1
port=8080
, we can use the following procedure to obtain the server configuration information:
Java code
The code is as follows |
Copy Code |
InputStream InputStream = This.getclass (). getClassLoader (). getResourceAsStream ("ipconfig.properties"); Properties P = new properties (); try { P.load (InputStream); catch (IOException E1) { E1.printstacktrace (); } System.out.println ("IP:" +p.getproperty ("IP") + ", Port:" +p.getproperty ("port")); |
This article only introduced some simple operation, the improper place hoped that everybody will advise