Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中一次性加载属性文件并在整个应用程序中使用它_Java_Spring_Properties - Fatal编程技术网

如何在java中一次性加载属性文件并在整个应用程序中使用它

如何在java中一次性加载属性文件并在整个应用程序中使用它,java,spring,properties,Java,Spring,Properties,我有一个场景,我正在尝试在我的应用程序中实现错误代码机制。这是我目前正在遵循的方法 Properties prop = new Properties(); InputStream input = null; try { String filename = "ErrorCode.properties"; input = getClass().getClassLoader().

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

    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);

                }
Properties prop=新属性();
InputStream输入=null;
试一试{
String filename=“ErrorCode.properties”;
输入=getClass().getClassLoader().getResourceAsStream(文件名);
如果(输入==null){
log.debug(“对不起,找不到”+文件名);
返回null;
}
道具载荷(输入);
枚举e=prop.propertyNames();
而(e.hasMoreElements()){
字符串键=(字符串)e.nextElement();
字符串值=prop.getProperty(键);
log.debug(“键:+Key+”,值:+Value);
}
但是我需要在很多不同的类中编写错误代码,我不想一直在不同的类中编写上面的代码

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

如何实现这一点?

实现这一目标的可能方法有哪些?

我正在使用spring,有没有办法在spring中实现这一点?


我支持任何其他机制,而不是属性文件。

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

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

定义一个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"));
}
在初始化属性之后,您可以在程序的任何地方使用它,只需引用常量类

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.
}

希望这能有所帮助。

定义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"));
}
在初始化属性之后,您可以在程序的任何地方使用它,只需引用常量类

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.
}

希望这能有所帮助。

如果是错误代码,您可能需要对其进行本地化-在这种情况下,请在此处查看解决方案:如果是错误代码,您可能需要对其进行本地化-在这种情况下,请在此处查看解决方案: