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

后端 未结 4 1137
礼貌的吻别
礼貌的吻别 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:15

    You can use JNDI if your app server supports it.

    See my question (and the answer) here: Spring deployment-level configuration

    0 讨论(0)
  • 2020-12-09 22:19

    Implement a ServletContextListener.

    Here's a basic kickoff example:

    public class Config implements ServletContextListener {
        private static final String ATTRIBUTE_NAME = "config";
        private Properties config = new Properties();
    
        @Override
        public void contextInitialized(ServletContextEvent event) {
            try {
                config.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
            } catch (IOException e) {
                throw new SomeRuntimeException("Loading config failed", e);
            }
            event.getServletContext().setAttribute(ATTRIBUTE_NAME, this);
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent event) {
            // NOOP.
        }
    
        public static Config getInstance(ServletContext context) {
            return (Config) context.getAttribute(ATTRIBUTE_NAME);
        }
    
        public String getProperty(String key) {
            return config.getProperty(key);
        }
    }
    

    which you register as follows in web.xml:

    <listener>
        <listener-class>com.example.Config</listener-class>
    </listener>
    

    and which you can access in your servlets as follows:

    Config config = Config.getInstance(getServletContext());
    String property = config.getProperty("somekey");
    

    After having a second thought, those properties are thus 100% specific to business layer, not to the webapplication itself? Then a ServletContextListener is indeed clumsy and too tight coupled. Just give the business layer its own Config class which loads the properties from the classpath and caches it in some static variable (Map<String, Properties> maybe?).

    0 讨论(0)
  • 2020-12-09 22:20

    Put your configuration classes/properties in a jar file, and put that jar file in WEB-INF/lib. Then you can access them through the normal classpath resource facilities whereever you need to in your web application.

    0 讨论(0)
  • 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");
    
    0 讨论(0)
提交回复
热议问题