3

in java, I am reading from a property file into a Properties object:

  static Properties fileProp = null;  // holds file properties
  public static void main( final String[] args ) throws IOException 
  {
     ...
    //load a properties file
    InputStream is = LearnButtonPressHttpHandler.class.getResourceAsStream("/rsrc/file.properties");

    fileProp.load(is);
    ...

    // more objects are created

  }

later, deeper into the code, several layers deeper, I need to access this properties object but find that I don't have access to this fileProp object. I don't want to pass this object as a parameter down the many layers to where I need it. It would solve the problem but does not seem to be such an elegant solution. Is there a better way?

user840930
  • 5,214
  • 21
  • 65
  • 94

4 Answers4

1

Create a static getter for the fileProp reference:

public static Properties getFileProp()
{
    return fileProp;
}

This way, any other piece of code that needs it, can access it.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • this really helped! Thanks! the other answers may also have worked, but this was the one that worked for me! – user840930 Dec 10 '13 at 08:18
  • Any time. Some of the other answers are good in terms of coding style for maintainability and design. My solution is quick and easy, good for a small project. – Mad Physicist Dec 10 '13 at 17:59
1

The simplest way in plain Java is to use the Singleton pattern, load your properties singleton at application startup or have a getProperties method in that class that returns the properties if they have been loaded and load them if not.

For example:

public class MySingleton {
   private static MySingleton instance = null;
   protected MySingleton() {
      // Exists only to defeat instantiation.
   }
   public static MySingleton getInstance() {
      if(instance == null) {
         instance = new MySingleton();
         instance.loadData()
      }
      return instance;
   }

   private void loadData(){
      //doSomething
   }
}

In this case, you can call MySingleton.getInstance(), and that will get you the data object that you want and not reload it if it was previously loaded. If it is the first time, it will load it lazily.

If you are using a framework or an application server, there is a multitude of ways depending on what your stack offers. E.g. in Spring, singleton is the default bean type.

amphibient
  • 29,770
  • 54
  • 146
  • 240
1

Create Singleton class which performs the property loading( I prefer Singleton rather than static class). Then you can access properties from any where.

public class Singleton {
    private static  Singleton INSTANCE = null;
    Properties props = new Properties();
    public static final Singleton getInstance() throws IOException {
         if (INSTANCE == null) {
             synchronized (Singleton.class) {
                 if (INSTANCE == null) {
                     INSTANCE = new Singleton();
                     INSTANCE.loadProperties();
                 }
             }
         }
        return INSTANCE;
    }
    private  void  loadProperties() throws IOException{
          InputStream is = Singleton.class.getResourceAsStream("/rsrc/file.properties");
          props.load(is);
    }
    public Object getData(String key){
        return props.get(key);
    }
}
Satheesh Cheveri
  • 3,621
  • 3
  • 27
  • 46
0

This is one of the major things dependency injection does. For instance, in Spring there is a PropertyPlaceholderConfigurer that is used to inject properties from one place into beans as they are being constructed (cf. the Prototype Pattern in Gang of Four).

Many frameworks just do static property management, e.g. Play. Then you are basically implementing a Singleton.

Rob
  • 11,446
  • 7
  • 39
  • 57