【问题标题】:How to load a property file once and use it throughout the application in java如何加载一次属性文件并在java中的整个应用程序中使用它
【发布时间】:2016-05-03 07:51:14
【问题描述】:

我有一个场景,我正在尝试在我的应用程序中实现错误代码机制。这是我目前正在遵循的方法。

    Properties prop = new Properties();
            InputStream input = null;

            try {

                String filename = "ErrorCode.properties";
                input = getClass().getClassLoader().getResourceAsStream(filename);
                if (input == null) {
                    log.debug("Sorry, unable to find " + filename);
                    return null;
                }

                prop.load(input);

                Enumeration<?> e = prop.propertyNames();
                while (e.hasMoreElements()) {
                    String key = (String) e.nextElement();
                    String value = prop.getProperty(key);
                    log.debug("Key : " + key + ", Value : " + value);

                }

但是我需要很多不同类的错误代码,我不想一直在不同的类中编写上述代码。

是否有任何其他方法可以将属性文件初始化一次并在类中的任何位置使用它。

我该如何实现?

有哪些不同的方法可以实现这一目标?

我用的是spring,spring有没有办法实现这个?

我愿意接受任何其他机制,而不是属性文件。

【问题讨论】:

标签: java spring properties


【解决方案1】:

为什么不直接实现一个单独的单例类,比如“ErrorCodes”,在对象创建期间初始化一次属性文件,然后通过 getter 检索文件?

【讨论】:

    【解决方案2】:

    定义一个 Constants.java 并添加您想要在整个应用程序运行时使用的属性。这些属性可以被其他类使用,并且不需要为每个类定义初始化。

    public class Constants {
    public static String[] tableAndColumnNames;
    public static String[] getTableAndColumnNames() {
            return tableAndColumnNames;
        }
        public static void setTableAndColumnNames(String tableNames) {
            Constants.tableAndColumnNames = tableNames.split(";");
        }
    
    public static String getDB() {
            return DB;
        }
    public static final String HSQLBD = "HSQLDB";
        public static final String ORACLE = "ORACLE";
    public static String DB;
        public static void setDB(int dB) {
            if (dB == 0) {
                Constants.DB = HSQLBD;
            } else {
                Constants.DB = ORACLE;
            }
        }
    }
    

    在应用程序启动时加载这些属性。当您加载属性时,只需执行如下设置的常量

    public static void loadProperties() {
    Properties property = new Properties();
                InputStream input = CommonUtilities.class.getResourceAsStream("/DB.properties");
                property.load(input);
                Constants.setDB(Integer.parseInt(prop.getProperty("DB")));
    Constants.setTableAndColumnNames(property.getProperty("tableAndColumnNames"));
    }
    

    在属性被初始化后,你可以在程序的任何时候使用它,只需引用 Constant 类。喜欢

    public void someOtherClass()
    
    public static void main(String[] args) {
        if(this.DB.equals(Constans.getDB()) // will return the DB from the properties class
        // no need to initialize properties again in every class.
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 2016-01-31
      • 2016-07-17
      • 2013-06-18
      相关资源
      最近更新 更多