Java Load Properties configuration file Tool class
Importorg.apache.commons.lang3.StringUtils;ImportOrg.apache.log4j.Logger;ImportJava.io.InputStream;ImportJava.util.HashMap;ImportJava.util.Map;Importjava.util.Properties;/*** Created by Yang on 2017/1/5. * Static Configuration*/ Public classConstants {Private StaticLogger Logger = Logger.getlogger (Constants.class); Private Staticmap<string, string> configmap =NULL; Static{Properties pt=NewProperties (); InputStream ist=NULL; Try{ist= Constants.class. getClassLoader (). getResourceAsStream ("Datasource-config.properties"); Pt.load (IST); Configmap=NewHashmap<string, string>(Pt.keyset (). Size ()); Logger.info ("Constants list:"); for(Object keyObj:pt.keySet ()) {String key=keyobj.tostring (); Object Objs=Pt.get (key); Logger.info (Key+ ":" +OBJS); Configmap.put (Key.trim (), objs.tostring ()); } } Catch(Exception e) {logger.error (E.getcause ()); } finally { Try { if(NULL!=ist) {Ist.close (); } } Catch(Exception e) {e.printstacktrace (); } } } Private Staticstring Getsimplevalue (String key, String defaultvalue) {String value=Configmap.get (key); if(Value = =NULL|| Value.trim (). Length () = = 0) {Value=DefaultValue; } assertStringutils.isnotblank (value); returnvalue; } Private Staticstring Getsimplevalue (String key) {returnConfigmap.get (key). ToString (); } Public StaticString Getestopichost () {returnGetsimplevalue ("DataSource.es.topic.host"); } Public StaticString Getesuserloghost () {returnGetsimplevalue ("DataSource.es.userlog.host"); } Public Static intgetshardedpoolmaxtotal () {returnInteger.valueof (Getsimplevalue ("Redis.shardedpool.maxtotal", "20") . Intvalue (); } Public Static LongGetshardedpoolmaxwaitmillis () {returnLong.valueof (Getsimplevalue ("Redis.shardedpool.maxwaitmillis", "1000") . Longvalue (); } //Redis Public StaticString Getredishost () {returnGetsimplevalue ("Redis.host"); } Public StaticString Getredisport () {returnGetsimplevalue ("Redis.port", "6379"); }}
Java Load Properties configuration file Tool class