繁体   English   中英

在单例类中读取大型xml文件的最佳方法是什么

[英]what is the best way to read a large xml file in a singleton class

我有一个单例类,该类从具有大量元素的xml文件中读取一些属性。当前,我正在读取单例类的构造函数中的xml文件。 读取xml中的条目后,我可以从单例实例访问那些条目,而不必一次又一次地读取xml。 我想知道这是正确的方法还是比这更好的方法来完成它。

如果要延迟加载属性,则可以编写以下类,它也将在多线程环境中工作。

class Singleton {
    private static Singleton instance;
    private Properties xmlProperties;

    private Singleton() {}

    public static Singleton getInstance() {
        if(instance == null) {
            synchronized(Singleton.class) {
                if(instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }

    public Properties getXmlProperties() {
        if(xmlProperties == null) {
            initProperties();
        }
        return xmlProperties;
    }

    private synchronized void initProperties() {
        if(xmlProperties == null) {
            //Initialize the properties from Xml properties file
            // xmlProperties = (Properties from XML file)
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM