加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java中的常量和属性

发布时间:2020-12-15 00:45:51 所属栏目:Java 来源:网络整理
导读:Java最佳实践建议将属性作为常量读取.那么,您认为达到目标的最佳方法是什么?我的方法是:一个Configuration类只读取一次属性文件(单例模式),并使用此类在需要时读取属性作为常量.并存储一个Constants类: 属性名称可在属性文件中找到它们(例如app.database.
Java最佳实践建议将属性作为常量读取.那么,您认为达到目标的最佳方法是什么?我的方法是:一个Configuration类只读取一次属性文件(单例模式),并使用此类在需要时读取属性作为常量.并存储一个Constants类:

>属性名称可在属性文件中找到它们(例如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中读取属性和常量的方法是什么?

提前致谢.

解决方法

我找到了一个更好的解决方案来均化代码并将所有内容都作为常量.使用相同的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";
}

然后Constant类将是:

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;

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读