config files for a webapplication - load once and store where?

后端 未结 4 1142
礼貌的吻别
礼貌的吻别 2020-12-09 21:46

I have a bunch of properties (configurations) that can change per environment. However these values do not change once the web application is deployed.So consider that there

4条回答
  •  隐瞒了意图╮
    2020-12-09 22:24

    You should have a static reference to these properties, and access them that way. You will end up reading them into memory once, and keeping them there. That way, you won't have to access the disk so many times at runtime.

    So, suppose you have a class AppProperties:

    public class AppProperties {
      private static Properties props;
    
      protected static loadProperties(File propsFile) {
        ... read from disk, and set props static member ...
      }
    
      public static getProperties() {
        return props;
      }
    }
    

    From your initializer servlet, you would call loadProperties to read the properties from disk. Then, in your application code, to access the properties:

    String myProp = AppProperties.getProperties().getProperty("myProp");
    

提交回复
热议问题