Records several ways to read the configuration file, along with the placement path of the configuration File.
1, using the Propertiesloaderutils tool (springframework package Provided)
Pros: load configuration files in real time, take effect immediately after modification, do not restart
Configuration file As for Classpath (with the class file, if the jar package needs to be hit in the package), the code is as Follows:
Private Static voidspringutil () {Properties props=NewProperties (); while(true){ Try{props=propertiesloaderutils.loadallproperties ("param.properties"); for(Object key:props.keySet ()) {System. out. Print (key+":"); System. out. println (props.Get(key)); } } Catch(ioexception E) {System. out. println (e.getmessage ()); } Try{thread.sleep ( the);}Catch(interruptedexception E) {e.printstacktrace ();} } }
2, according to the file path read
Advantage: configuration files can be placed outside the jar package, looking for configuration files based on file path
The code is as Follows:
public Staticstring ReadValue (string fileName, string key) {Properties props=NewProperties (); String value=NULL; Try { //the configuration file is located under the Config directory in the current directoryInputStreaminch=NewBufferedinputstream (NewFileInputStream ("config/"+fileName)); Props.load (inch); Value=Props.getproperty (key); } Catch(Exception E) {e.printstacktrace (); } returnvalue;}
3. Spring load configuration file in two ways
1) press Classpath to load (configuration file and class file in the same Directory)
To initialize the spring code:
public StaticBoolean initialize () {if(isinitialize) {return true; } Try{APPCONTENXT=NewFilesystemxmlapplicationcontext ("Spring.xml"); Isinitialize=true; return true; } Catch(Exception e) {logger.error ("Initialize Spring Framework Failed.", e); return false; }}
Configuration file Format:
<bean id="propertyconfigurer" class=" Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property Name ="locations"> <list> <value>jdbc.properties</value > <value>param.properties</value> </list> </property></bean>
Ibatis Configuration Notation:
<sqlMapConfig> <settings cachemodelsenabled="true"Enhancementenabled="true"lazyloadingenabled="true"Errortracingenabled="true"maxrequests="1024x768"maxSessions=" +"maxtransactions=" -"usestatementnamespaces="true"/> <sqlmap resource="Sqlmap/sqlmap-global.xml"/> <sqlmap resource="Sqlmap/sqlmap-memo.xml"/> <sqlmap resource="Sqlmap/sqlmap-city.xml"/></sqlmapconfig>
2) load by file path (E.G. configuration file is located in config directory in current Directory)
To initialize the spring code:
public StaticBoolean initialize () {if(isinitialize) {return true; } Try{APPCONTENXT=NewFilesystemxmlapplicationcontext ("File:config/spring.xml"); Isinitialize=true; return true; } Catch(Exception e) {logger.error ("Initialize Spring Framework Failed.", e); return false; } }
Configuration file Format:
<bean id="propertyconfigurer" class=" Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property Name ="locations"> <list> <value>file:config/ jdbc.properties</value> <value>file:config/param.properties</value> </list> </property></bean>
Ibatis Configuration Notation:
<sqlMapConfig> <settings cachemodelsenabled="true"Enhancementenabled="true"lazyloadingenabled="true"Errortracingenabled="true"maxrequests="1024x768"maxSessions=" +"maxtransactions=" -"usestatementnamespaces="true"/> <sqlmap url="File:config/sqlmap/sqlmap-global.xml"/> <sqlmap url="File:config/sqlmap/sqlmap-windcustomcode.xml"/> <sqlmap url="File:config/sqlmap/sqlmap-shiborprices.xml"/></sqlmapconfig>
Java configuration file read and path settings