Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid reading property file multiple times

Tags:

java

We have some data in properties file. This data is used across many classes. So, we create a Properties class object in each and every class and then read data using getProperty() method. This is leading to duplication of code.

Can someone please suggest some best practices to avoid this?

One thing that came to my mind is:
Create a class
Have a public variable for each property in property file in this class
Have a method that assigns values to each and every property
In the class where property values are required, create an object for this class and access the public variables

But, things i don't like with this approach are public variables and if at all a new property is added to the property file, i need to add code to read that property in the class.

Any help is appreciated.

Thank you!

like image 331
Austin Avatar asked Aug 29 '13 17:08

Austin


People also ask

Which method helps in reading properties file?

load() method of Properties class is convenient to load . properties file in the form of key-value pairs.


2 Answers

You can create a Singleton class, that loads the properties the first time it gets invoked.. and a public method that retrieves the property value, for a given property key..

This is assuming you're using a standart Properties file... But you can extrapolate this to any key-value pair, changing Properties type to a Map or something else.

Something like

public class PropertyHandler{

   private static PropertyHandler instance = null;

   private Properties props = null;

   private PropertyHandler(){
         // Here you could read the file into props object
         this.props = ..... 
   }

   public static synchronized PropertyHandler getInstance(){
       if (instance == null)
           instance = new PropertyHandler();
       return instance;
   }

   public String getValue(String propKey){
       return this.props.getProperty(propKey);
   }
}

Then you can invoke this as needed.. from any code.. like this.

String myValue = PropertyHandler.getInstance().getValue(propKey);

Hope this helps

like image 141
Cristian Meneses Avatar answered Nov 10 '22 22:11

Cristian Meneses


for me static inner class is the best possible way to do it. It will do it with lazily, as class loading is synchronized so thread safe, plus performant also. So with this we are achieving three things:

  1. good performance because with synchronizing the liveliness will suffer, but here we are using static inner class.
  2. thread safety because when inner class will be loaded than only map will be initialized as the class loading is thread safe hence all total thread safe.
  3. Inner class will be loaded when we will call Singleton.initialize().get(key) so the map gets initialized lazily.

Below is the code...

public class SingletonFactory 
{   
    private static class Singleton
    {
        private static final Map<String, String> map = new HashMap<String, String>();
        static
        {
            try
            {
                //here we can read properties files
                map.put("KEY", "VALUE");
            }
            catch(Exception e)
            {
                //we can do the exception handling
                System.out.println(e);
            }
        }
        private static Map<String, String> initialize()
        {   
            return map;
        }
    }

    public static String getValue(String key)
    {
        return Singleton.initialize().get(key);
    }
}
like image 28
Trying Avatar answered Nov 10 '22 22:11

Trying