java Web 应用程序的配置文件 - 加载一次并存储在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3153739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:36:22  来源:igfitidea点击:

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

javaservletsproperties

提问by Kapsh

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.

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

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.

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

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?

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

Thanks.

谢谢。

回答by BalusC

Implement a ServletContextListener.

实施一个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:

您注册如下web.xml

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

and which you can access in your servlets as follows:

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

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 ServletContextListeneris indeed clumsy and too tight coupled. Just give the business layer its own Configclass which loads the properties from the classpath and caches it in some staticvariable (Map<String, Properties>maybe?).

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

回答by pkaeding

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:

所以,假设你有一个类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 loadPropertiesto read the properties from disk. Then, in your application code, to access the properties:

从您的初始化 servlet,您将调用loadProperties从磁盘读取属性。然后,在您的应用程序代码中,访问属性:

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

回答by Thorbj?rn Ravn Andersen

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.

将您的配置类/属性放在一个 jar 文件中,并将该 jar 文件放在 WEB-INF/lib 中。然后,您可以通过 Web 应用程序中任何需要的普通类路径资源工具访问它们。

回答by Robert Wilson

You can use JNDI if your app server supports it.

如果您的应用服务器支持,您可以使用 JNDI。

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

在此处查看我的问题(和答案):Spring 部署级配置