Java configuration file read and path settings

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.