【问题标题】:Constants and properties in javajava中的常量和属性
【发布时间】:2015-07-05 23:25:36
【问题描述】:

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

  • 要在属性文件中找到它们的属性名称(例如 app.database.url)。
  • 静态常量(我不希望用户配置的常量,例如 CONSTANT_URL="myurl.com")。
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();
}}

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

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 中读取属性和常量的方法是什么?

提前致谢。

【问题讨论】:

    标签: java configuration constants config properties-file


    【解决方案1】:

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

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

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

    然后常量类将是:

    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;
    

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

    【讨论】:

    • 正如 Fabien 所评论的,它也可能将 getProperty 功能放入 getInstance 并将其重命名为新的 getProperty 以避免调用 getInstance().getProperty 但我将其留在帖子中,因为它更容易理解。
    【解决方案2】:

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

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

    【讨论】:

    • 不客气。我向您推荐了这个解决方案,因为我知道我们有时需要一个允许从不同支持获取属性的解决方案。否则,您采用的解决方案似乎也不错。我只需将getProperty 设置为static 并在其中包含getInstance,以避免在需要检索属性的方法中调用getInstance
    • 感谢您的回复,它是一个强大的 JNDI 或 XML 库,但我认为简单的属性文件比 java.util 的简单性更可取。由于 java.util.Properties 实现了 Map,因此还可以添加多个源以加载新资源并使用 Properties.putAll 方法添加。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多