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

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

问题描述

我有一堆可以根据环境更改的属性(配置)。但是,一旦部署了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等无关,并且不依赖于webapp。

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< String,Properties> 也许?)。


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

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

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