This article will introduce some methods for reading configuration files from java, including reading configuration files from spring and from the configuration package of Jakarta Commons. I hope this will be helpful to all of you.
The configuration package of Jakarta Commons reads the configuration file.
There are two common Configuration files: Key-Value Pair format, or XML Configuration file. You can use the Commons Configuration package to read such Configuration files.
The key-Value Pair format is also a 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, reading and parsing XML files is also so simple:
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 ); } } |
From the code above, we can see that the getXXX () method of XMLConfiguration also supports XPath to parse XML files.
In addition, you can use XML and attribute files to read configuration files. For example:
The configurations of an application are different. First, it is a global configuration. Then, local configuration may be required somewhere. When the two are repeated, the local configuration prevails. Then there is a user-defined configuration file, the priority of the custom configuration file is higher than that of local configuration and global configuration. In this case, you can use a ConfigurationFactory to manage the configuration files, and define the three different configuration files as global. properties, local. properties, user. properties, and then use a configuration. xml file to manage the priority relationship between the three, as shown below:
Global. properties, one of the property configuration files
Threads. max = 50threas. min = 2
Timout = 15.52.
Interactive = true
Color = red
Speed = 50
Name = Default User
Property configuration file 2 local. properties
Threads. max = 30 speed = 55
Property configuration file 3 user. properties
Threads. min = 1 color = black
Speed = 5000
Name = Sean
Configuration. xml configuration file for managing these three Property configuration files
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> |
Then, you can use the following in the 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 output result is 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 the configuration file
1. Read the xml configuration file
(1) create a 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; } }
|
(2) construct a configuration file (beanConfig. xml)
Xml Code
The 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> |
(3) Reading xml files
1. Use 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. Read data using FileSystemResource
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 when FileSystemResource is used, the configuration file must be placed in the direct directory of the project, or the absolute path must be specified. Otherwise, an exception that cannot be found will be thrown.
2. Read the properties configuration file
Two technologies are introduced here: Reading properties files using spring and reading with java. util. Properties
(1) Use spring to read the properties File
We also use the preceding HelloBean. java file 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 Bean alias setting, and. class is used to specify the class source.
Use org. springframework. beans. factory. support. PropertiesBeanDefinitionReader to read the attribute file.
Java code
The 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 ()); |
(2) Using java. util. Properties to read attribute files
For example, we construct ipConfig. properties to save the Server IP address and port, for example:
Properties code
Ip = 192.168.0.1
Port = 8080
Then, we can use the following program to obtain 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 introduces some simple operations. I hope you will give me more advice on any improper operations.