in the real work, we often need to save some system configuration information, we generally will choose to complete the configuration file, this article according to the author's work to read the configuration file of a small sum, the main description is spring read the configuration file method.
Reading an XML configuration file
(a) Create a new Java bean
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
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd" >
<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
ApplicationContext context = new Classpathxmlapplicationcontext ("Beanconfig.xml");
Hellobean Hellobean = (Hellobean) context.getbean ("Hellobean");
System.out.println (Hellobean.gethelloworld ());
2. Using Filesystemresource to read
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 the exception that cannot find the file will be thrown.
Second, read the 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:
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
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:
ip=192.168.0.1
port=8080
, we can use the following procedure to obtain the server configuration information:
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"));