Web 应用程序的配置文件 - 加载一次并存储在哪里? [英] config files for a webapplication - load once and store where?

查看:14
本文介绍了Web 应用程序的配置文件 - 加载一次并存储在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆属性(配置)可以根据环境改变.但是,一旦部署了 Web 应用程序,这些值就不会更改.因此,请考虑在正常程序流程中我想多次读取 application.properties 文件.

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 is an application.properties file that I want to read lots of times during normal program flow.

我知道我可以在服务器启动时加载这些.但是,就从后端的简单 Java 类访问这些而言,最佳实践是什么?这些业务类与 servlet 等无关,也不依赖于 Web 应用程序.

I know that I can probably load these at server startup time. However whats the best practice as far as accessing these from simple java classes at the backend? These business classes have nothing to do with servlets etc and have no dependencies on a webapp.

所以今天我通过 ServletContext 加载属性.然后呢?我应该把它们放在哪里,以便其他对象可以轻松访问而无需再次执行 fileInputStream.load?

So today I load the properties via a ServletContext. Then what? Where should I keep them so as to be easily accessible to other objects without having to do a fileInputStream.load again?

谢谢.

推荐答案

实施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);
    }
}

你在web.xml中注册如下:

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

您可以在您的 servlet 中访问它,如下所示:

and which you can access in your servlets as follows:

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

<小时>

经过深思熟虑后,这些属性因此 100% 特定于业务层,而不是 Web 应用程序本身?那么 ServletContextListener 确实很笨拙,而且耦合太紧.只需为业务层提供自己的 Config 类,该类从类路径加载属性并将其缓存在某个 static 变量中(Map 也许?).


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?).

这篇关于Web 应用程序的配置文件 - 加载一次并存储在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆