From: http://blog.csdn.net/hbcui1984/archive/2007/02/28/1516911.aspx
In actual work, we often need to save some system configuration information. Generally, you can select a configuration file to complete the process, based on the methods used in my work to read the configuration file, this article briefly describes how spring reads the configuration file.
1. Read the xml configuration file
(1) create a 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;
}
}
(2) 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>
(3) Reading XML files
1. Use classpathxmlapplicationcontext
Applicationcontext context = new classpathxmlapplicationcontext ("beanconfig. xml ");
Hellobean = (hellobean) Context. getbean ("hellobean ");
System. Out. println (hellobean. gethelloworld ());
2. Read data using filesystemresource
Resource rs = new filesystemresource ("D:/software/tomcat/webapps/springwebdemo/WEB-INF/classes/beanconfig. xml ");
Beanfactory factory = new xmlbeanfactory (RS );
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 using Java. util. properties reading (1) Using spring to read the properties file we also use hellobean above. java file, which is constructed as follows: beanconfig. properties file:
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.
Beandefinitionregistry Reg = new defaultlistablebeanfactory ();
Propertiesbeandefinitionreader reader = new propertiesbeandefinitionreader (REG );
Reader. loadbeandefinitions (New classpathresource ("beanconfig. properties "));
Beanfactory factory = (beanfactory) reg;
Hellobean = (hellobean) Factory. getbean ("hellobean ");
System. out. println (hellobean. gethelloworld (); (2) use Java. util. properties to read the property file. For example, we construct an ipconfig. properties to save the Server IP address and port, such:
IP = 192.168.0.1
Port = 8080
Then, we can use the following program to obtain the server configuration information: 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 "));
How to Set Proxy:
Properties props = system. getproperties ();
Props. setproperty ("proxyset", "true ");
Props. setproperty ("HTTP. proxyhost", "192.168.0.200 ");
Props. setproperty ("HTTP. proxyport", "3128 ");