java 中的常量和属性

Constants and properties in java

Java 最佳实践建议将属性读取为常量。那么,您认为实现它的最佳方法是什么?我的方法是:配置 class 只读取属性文件一次(单例模式),并在需要时使用此 class 读取属性作为常量。和一个常量 class 来存储:

public final class Configurations {

private Properties properties = null;
private static Configurations instance = null;

/** Private constructor */
private Configurations (){
    this.properties = new Properties();
    try{
        properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(Constants.PATH_CONFFILE));
    }catch(Exception ex){
        ex.printStackTrace();
    }
}   

/** Creates the instance is synchronized to avoid multithreads problems */
private synchronized static void createInstance () {
    if (instance == null) { 
        instance = new Configurations ();
    }
}

/** Get the properties instance. Uses singleton pattern */
public static Configurations getInstance(){
    // Uses singleton pattern to guarantee the creation of only one instance
    if(instance == null) {
        createInstance();
    }
    return instance;
}

/** Get a property of the property file */
public String getProperty(String key){
    String result = null;
    if(key !=null && !key.trim().isEmpty()){
        result = this.properties.getProperty(key);
    }
    return result;
}

/** Override the clone method to ensure the "unique instance" requeriment of this class */
public Object clone() throws CloneNotSupportedException {
    throw new CloneNotSupportedException();
}}

常量 class 包含对属性和常量的引用。

public class Constants {
// Properties (user configurable)
public static final String DB_URL = "db.url";
public static final String DB_DRIVER = "db.driver";

// Constants (not user configurable)
public static final String PATH_CONFFILE = "config/config.properties";
public static final int MYCONSTANT_ONE = 1;
}

属性文件为:

db.url=www.myurl.com
db.driver=mysql

读取属性和常量为:

// Constants
int i = Constants.MYCONSTANT_ONE;
// Properties
String url = Configurations.getInstance().getProperty(Constants.DB_URL);

您认为这是一个好方法吗?您在 Java 中读取属性和常量的方式是什么?

提前致谢。

我认为您应该看看 Apache Commons Configuration,您会发现它很有用。您可以做很多事情,例如为您的配置文件设置层次结构,指定多个来源。

对于常量来说似乎不错:)

我找到了一个更好的解决方案来统一代码并将所有内容都作为常量。使用相同的配置 class 和 .properties 文件:由于 getInstance() 方法是静态的,因此可以在 class 常量中初始化常量。

A class 将名称存储到 .properties 文件中的 属性:

public class Properties {
// Properties (user configurable)
public static final String DB_URL = "db.url";
public static final String DB_DRIVER = "db.driver";
}

然后常量 class 将是:

public class Constants {
// Properties (user configurable)
public static final String DB_URL = Configurations.getInstance().getProperty(Properties.DB_URL);
public static final String DB_DRIVER = Configurations.getInstance().getProperty(Properties.DB_DRIVER );

// Constants (not user configurable)
public static final String PATH_CONFFILE = "config/config.properties";
public static final int MYCONSTANT_ONE = 1;
}

最后使用它们:

// Constants
int i = Constants.MYCONSTANT_ONE;
// Properties
String url = Constants.DB_URL;

我认为这是一个干净而优雅的解决方案,可以修复测试中的常量和属性问题。