何时读取 Java Web 应用程序中的 .properties 文件

发布于 2024-10-05 16:42:09 字数 977 浏览 3 评论 0原文

我正在使用 Java EE6 和 JSF 2.0 开发一个 Web 应用程序。我有一个 .properties 文件,其中存储了一些常量,我需要在部署应用程序之前轻松更改这些常量。我知道如何读取属性文件,但问题是何时读取?

这是我用来访问名为 CLIENT_IDCLIENT_SECRET 的属性的类:

public class FConnectProperties {

    Properties properties;

    public FConnectProperties() throws FileNotFoundException, IOException{
        properties = new Properties();
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        InputStream stream = classLoader.getResourceAsStream("/FConnect/FConnect.properties");
        properties.load(stream);
        stream.close();
    }

    public String getClientID(){
        return properties.getProperty("CLIENT_ID");
    }

    public String getClientSecret(){
        return properties.getProperty("CLIENT_SECRET");
    }

由于此类从磁盘读取属性文件,因此每次我都可以实例化它吗?需要访问它们吗?或者我应该在应用程序启动期间实例化它一次(可能从 Singleton Bean)并从那里访问它?这样做的最佳方法是什么?

应用程序启动后,这些属性就不会更改。我只是阅读它们,从不更新它们。

I'm developing a web application using Java EE6 and JSF 2.0. I have a .properties file in which I am storing a few constants, which I need to change easily before deploying the app. I know how to read a properties file, but the question is when?

This is the class I'm using to access the properties called CLIENT_ID and CLIENT_SECRET:

public class FConnectProperties {

    Properties properties;

    public FConnectProperties() throws FileNotFoundException, IOException{
        properties = new Properties();
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        InputStream stream = classLoader.getResourceAsStream("/FConnect/FConnect.properties");
        properties.load(stream);
        stream.close();
    }

    public String getClientID(){
        return properties.getProperty("CLIENT_ID");
    }

    public String getClientSecret(){
        return properties.getProperty("CLIENT_SECRET");
    }

Since this class reads the properties file from disk, is it ok to instantiate it every time I need to access them? Or should I instantiate it once during application startup, (possibly from a Singleton Bean) and access it from there? What is the best way to go about doing this?

The properties don't change once the application is launched. I'm only reading them, never updating them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他夏了夏天 2024-10-12 16:42:09

我建议在启动时读取一次文件,并正如您所说的将它们保存在一个单例中。由于我猜测您的属性是键/值对的选择,它们并不是那么大,所以我认为它们应该在地图类型数据结构中表现良好。

根据您需要访问它们的频率,从磁盘读取它们可能会变得昂贵。

I would suggest reading the file once at startup, and as you say holding them in a singleton. Since I'm guessing your properties are a selection of key/value pairs, that aren't all that large, I'd say they should do well in a map-type data structure.

Depending on how often you need to access them, then reading them from disk could become costly.

听风吹 2024-10-12 16:42:09

由于属性文件不会更改,因此仅创建一个应在启动时加载的文件实例是有意义的。无需再次读取该文件。

Since the properties file don't change, it makes sense to make only one instance of the file that should be loaded at startup. There is no need to read the file again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文