简体   繁体   中英

.properties as final static variable

I've got a .properties in my java project, that is updated by shell script. I want to get this properties values and use it as final static variable. Here is my .properties code

 WEBURL=http://popgom.fr
 NODEURL=http://192.168.2.30:5555/wd/hub

I know I can get and use my .properties using this :

Properties prop = new Properties();
InputStream input = new FileInputStream("config.properties");
// load a properties file
prop.load(input);

String urlnode = prop.getProperty("NODEURL");

What I want to do is to get this String in every class of my project, without adding the code bellow in every classes.

How can I do this ? I've try to do an Interface, without success.

Any of you have an idea ?

Thanks for help

you can do it using Singleton pattern:

Example:

public class MyProperties{
   private static MyProperties instance = null;
   private Properties prop;

   private MyProperties() {
      InputStream input = new FileInputStream("config.properties");
      // load a properties file
      prop.load(input);

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

   public Properties getProperty() {
       return prop;
   }
}

You can invoke this code everywhere:

....
....
MyProperties.getInstance().getProperty();
....
....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM