Transferred from: http://www.iteye.com/topic/56496
In the real work, we often need to save some system configuration information, we generally choose the configuration file to complete, this article according to the author's work in the use of the method of reading the configuration file a small summary, the main description is spring read the configuration file method.
I. Reading an XML configuration file
(i) Create a new Java bean (Hellobean.java)
Java 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) construct a configuration file (Beanconfig.xml)
XML 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 an XML file
1. Using Classpathxmlapplicationcontext
Java code
- ApplicationContext context = new Classpathxmlapplicationcontext ("Beanconfig.xml");
- Hellobean Hellobean = (Hellobean) context.getbean ("Hellobean");
- System.out.println (Hellobean.gethelloworld ());
2. Read with Filesystemresource
Java 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 important to note that, with Filesystemresource, the configuration file must be placed in the project direct directory, or an absolute path, or an exception will be thrown that cannot find the file.
Two. Read the properties configuration file
Here are two techniques: using spring to read the properties file and to read with Java.util.Properties
(i) using spring to read the properties file
We also use the Hellobean.java file above to construct the following beanconfig.properties file:
Properties Code
- Hellobean.class=chb.demo.vo.hellobean
- hellobean.helloworld=hello!chb!
The "Hellobean" name in the properties file is the Bean's alias setting, and the. Class is used to specify the source of the class.
Then use Org.springframework.beans.factory.support.PropertiesBeanDefinitionReader to read the properties file
Java 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 the properties file
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
Then we can use the following procedure to obtain the server configuration information:
Java 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 a few simple operations, the wrong place I hope you have a lot of advice
RPM: Several ways Java reads a configuration file