ImportJava.io.BufferedInputStream;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.net.URL;Importjava.util.ArrayList;Importjava.util.Enumeration;ImportJava.util.HashMap;Importjava.util.List;ImportOrg.apache.commons.logging.Log;Importorg.apache.commons.logging.LogFactory;Importorg.dom4j.Document;Importorg.dom4j.Element;ImportOrg.dom4j.io.SAXReader;/*** Load Config file **/ Public classpropertyfactory{Private StaticLog logger = Logfactory.getlog (propertyfactory.class); Private StaticJava.util.Properties Pros =Newjava.util.Properties (); PublicPropertyfactory (list<string>filepaths) { if(filepaths!=NULL){ for(intI=0;i<filepaths.size (); i++) {String FilePath=Filepaths.get (i); InputStream in=NULL; URL URL=Thread.CurrentThread (). Getcontextclassloader (). getresource (FilePath); if(NULL==URL) {URL= This. GetClass (). getClassLoader (). getresource (FilePath); } if(NULL==URL) {URL= This. GetClass (). getresource (FilePath); } //troubleshoot file paths where spaces may occurString Path =Url.getfile (); if(!"". Equals (Path) {Path= Path.replace ("%20", "" "); } Try{ in=NewBufferedinputstream (NewFileInputStream (path)); Pros.load (in); } Catch(Exception e) {logger.error ("Error loading configuration file: \ n", E); } finally { if(In! =NULL) { Try{in.close (); } Catch(IOException e) {logger.error ("Off input error: \ n", E); } } } } } } Public Staticstring GetProperty (String key) {returnPros.getproperty (key); } Public voidSetProperty (string key, String value) {Pros.setproperty (key, value); }}
Add Key,value to the configuration file
Need to inject applicationcontext.xml in spring config file
<!--read Profile class--
<bean id= "Propertyfactory" class= "Com.creditease.autoloan.common.PropertyFactory" >
<constructor-arg>
<list>
<value>/config.properties</value>
<value>/global.properties</value>
</list>
</constructor-arg>
</bean>
Call Propertyfactory.getproperty directly ("XXX") when used.
Java Read configuration file