Thursday, October 30, 2014

How to read Configuration Property File Keys : Java configuration.properties File

Removed

I needed some predefined values in my system. For that, I have declared a property file containing all these values. My configuration.properties looks like this:



1
2
3
threads=5
configurationProfile=default_profile
autostartup=true


In order to load this property file using Java, we need first to put the file under: src/main/resources. Then we use this code:


private Properties configProp=null;
...
public String getProperty(String key, String returnType) throws LoaderException {
 String result = null;
 // the returnType can only be null or STRING return type  
 if((returnType!=null)&&(!returnType.equalsIgnoreCase(STRING_RETURN_TYPE)))
 {
  logger.error("Invalid return type, should be string !! ");
  throw new LoaderException(" Invalid return type, should be string !! ", LoaderErrorEnum.RETURN_TYPE_ERROR);
 }
 try {
  loadFileInMemory();
  result = this.configProp.getProperty(key);
 } catch (Exception e) {
  logger.error("EXCEPTION WHILE GETTING PROPERTY ["+key+ "]" , e);
 }

 return result;
}


So in order to get a property by Key, we need to use :


public String getProperty(String key, String returnType) throws LoaderException {
 String result = null;
 
 if((returnType!=null)&&(!returnType.equalsIgnoreCase(STRING_RETURN_TYPE)))
 {
  logger.error("Invalid return type, should be string !! ");
  throw new LoaderException(" Invalid return type, should be string !! ", LoaderErrorEnum.RETURN_TYPE_ERROR);
 }
 try {
  loadFileInMemory();
  result = this.configProp.getProperty(key);
 } catch (Exception e) {
  logger.error("EXCEPTION WHILE GETTING PROPERTY ["+key+ "]" , e);
 }

 return result;
}


That's all for today. Don't forget to share :)

No comments :

Post a Comment

Articles les plus consultés