Transferred from: http://blog.csdn.net/stypace/article/details/38414871
First, the use of org.apache.commons.configuration
You need to use a jar package: Commons-collections-3.2.1.jar, Commons-configuration-1.10.jar, Commons-lang-2.6.jar and Commons-logging-1.2.jar.
Configuration files that can be read: XML and properties
1. Read the XML file
[Java]View PlainCopy
- <span style="Font-family:microsoft yahei;font-size:12px;" > Packagecom.styspace;
- Import org.apache.commons.configuration.Configuration;
- Import org.apache.commons.configuration.ConfigurationException;
- Import org.apache.commons.configuration.XMLConfiguration;
- Public class Xmlloadertest {
- public static void Main (string[] args) throws configurationexception{
- Configuration config = new xmlconfiguration ("Com/styspace/config.xml");
- String name = config.getstring ("Account.name");
- System.out.println ("Name:" + name);
- }
- }
- </span>
It is important to note that the parameter in config.getstring ("Account.name") is Account.name, which is in XPath format and cannot contain the root element in the XML.
The contents of the. config to use are as follows:
[HTML]View PlainCopy
- <span style="Font-family:microsoft yahei;font-size:12px;" ><? XML version= "1.0" encoding="GBK"?>
- <Accounts>
- <account type="by0003">
- <code>100001</code>
- <pass>123</pass>
- <name> John Doe </name>
- <money>1000000.00</money>
- </Account>
- </Accounts></span>
2. Read the properties file
[Java]View PlainCopy
- <span style="Font-family:microsoft yahei;font-size:12px;" > Packagecom.styspace;
- Import org.apache.commons.configuration.Configuration;
- Import org.apache.commons.configuration.ConfigurationException;
- Import org.apache.commons.configuration.PropertiesConfiguration;
- Public class Peropertiesloadertest {
- public static void Main (string[] args) throws configurationexception{
- Configuration config = new propertiesconfiguration ("com/styspace/config.properties");
- String name = config.getstring ("name");
- System.out.println ("Name:" + name);
- }
- }
- </span>
The contents of the Config.properties file used are as follows:
[Plain]View PlainCopy
- <span style= "Font-family:microsoft yahei;font-size:12px;" >threads.max=50threas.min=2
- timout=15.52
- Interactive=true
- Color=red
- Speed=50
- Name=default user</span>
Second, using java.util.Properties read
[Java]View PlainCopy
- <span style="Font-family:microsoft yahei;font-size:12px;" > Packagecom.styspace;
- Import java.io.IOException;
- Import Java.io.InputStream;
- Import java.util.Properties;
- Public class Propertiestest {
- public static void Main (string[] args) {
- Propertiestest pt = new Propertiestest ();
- try {
- Pt.getproperties ();
- } catch (IOException e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- }
- private void GetProperties () throws IOException {
- InputStream InputStream = this.getclass (). getClassLoader (). getResourceAsStream ("com/styspace/ Config.properties ");
- SYSTEM.OUT.PRINTLN ("BEGIN!!!");
- Properties Properties = new properties ();
- try{
- Properties.load (InputStream);
- }catch (IOException IoE) {
- Ioe.printstacktrace ();
- }finally{
- Inputstream.close ();
- }
- System.out.println ("Name:" +properties.getproperty ("name"));
- }
- }
- </span>
It is important to note that the Hetclassloader (). getResourceAsStream () parameter is the path under the project root directory, Although Config.properties is the same type of file in the same directory, it cannot be written as getClassLoader (). getResourceAsStream ("Config.properties"), so the program will error, The resulting inputstream is a null value.
ClassLoader () and URLClassLoader () Differences: ClassLoader () can only find files in the SRC directory, while URLClassLoader () can find files in any directory.
Third, the read of the configuration file in spring
1. Classpathxmlapplicationcontext: Loaded from the classpath.
2. Filesystemxmlapplicationcontext: Load from File system.
3, Xmlwebapplicationcontext: Load from the web system.
1. Get Beans using bean factory
[Java]View PlainCopy
- <span style="Font-family:microsoft yahei;font-size:12px;" > beanfactory factory = null; //Statement
- Classpathresource resource = new Classpathresource ("Spring.xml"); Class path
- factory= New Xmlbeanfactory (Resource);
- Filesystemresource Filesystemresource = new Filesystemresource ("D:\\ncode\\mcode\\sday02\\src\\spring.xml" ); //file path
- factory= New Xmlbeanfactory (Filesystemresource);
- //xmlbeanfactory (Parameters can be resource or filesystemresource, etc.
- //But cannot be res reason can be viewed: Document part Iii. Core Technologies 6. Resources
- //6.2 The Resource interface The description of the IsOpen method);
- //inputstreamresource res = new Inputstreamresource (New FileInputStream ("d:\\ncode\\mcode\\sday02\\src\\ Spring.xml "));//system path
- HelloService HelloService = Factory.getbean ("Helloserviceimpl", Helloserviceimpl. Class);
- Helloservice.sayhello ();</span>
2. Use context
More advanced Context: Provides text information parsing tools, including support for internationalization, provides a common method for loading file resources, and can send events to beans registered as listeners.
In rare cases, use beanfactory.
[Java]View PlainCopy
- <span style="Font-family:microsoft yahei;font-size:12px;" > //From File system
- ApplicationContext context = new Filesystemxmlapplicationcontext ("file:d:\\ncode\\mcode\\sday02\\src\\ Spring.xml ");
- //From class path
- ApplicationContext context = new Classpathxmlapplicationcontext ("Classpath:spring.xml");
- HelloService HelloService = Context.getbean ("Helloserviceimpl", Helloserviceimpl. Class);
- Helloservice.sayhello ();</span>
3. Use in Web applications
3.1. Using Xmlwebapplicationcontext
[Java]View PlainCopy
- Xmlwebapplicationcontext context = new Xmlwebapplicationcontext ();
- The default path/web-inf/applicationcontext.xml
- Applicationcontext.xml file name can be any
- Re-set the path
- Context.setconfiglocations (new string[] {"/web-inf/classes/applicationcontext.xml"});
- Set the context for the ServletContext context for the Web application
- Context.setservletcontext (Getservletcontext ());
- Refresh
- Context.refresh ();
- Get by ID Name
- Hellodao Hellodao = Context.getbean ("Hellodaoimpl", Hellodaoimpl. Class);
- Methods for executing Hellodao objects
- Hellodao.sayhello ();
3.2. Using Webapplicationcontextutils Tool class
[Java]View PlainCopy
- Get the context object directly using Getwebapplicationcontext (Getservletcontext ())
- Webapplicationcontext context=
- Webapplicationcontextutils.getwebapplicationcontext (Getservletcontext ());
- Context = Webapplicationcontextutils.getrequiredwebapplicationcontext (Getservletcontext ());
- SYSTEM.OUT.PRINTLN (context);
- Hellodao Hellodao = Context.getbean ("Hellodaoimpl", Hellodaoimpl. Class);
- Hellodao.sayhello ()
The difference between the two is:
1, when using Getwebapplicationcontext (Getservletcontext ()) to obtain the context object, the output of the context object is null, so in use
Context.getbean ("Hellodaoimpl", Hellodaoimpl.class); the exception of a null pointer appears
2, when the use of Getrequiredwebapplicationcontext (Getservletcontext ()); Get the context object when the following bug occurs
Java.lang.IllegalStateException:No Webapplicationcontext found:no Contextloaderlistener registered
How to read a configuration file in Java