Constants and attributes in Java
Java best practices recommend reading properties as constants So what do you think is the best way to achieve your goal? My method is: a configuration class reads the property file only once (singleton mode), and uses this class to read the property as a constant when needed And store a constants class:
>Attribute names can be found in the attribute file (for example, app. Database. URL). > Static constants (I don't want user configured static constants, such as 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();
}}
The constant class contains references to properties and constants
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;
}
The properties file will be:
db.url=www.myurl.com db.driver=MysqL
The properties and constants to read will be:
// Constants int i = Constants.MYCONSTANT_ONE; // Properties String url = Configurations.getInstance().getProperty(Constants.DB_URL);
Do you think this is a good way? What are the methods to read properties and constants in Java?
Thank you in advance
Solution
I found a better solution to homogenize the code and treat everything as a constant Use the same configurations class and Properties file: since getInstance () method is static, constants can be initialized in class constants
Used to store names in Classes of properties in the properties file:
public class Properties {
// Properties (user configurable)
public static final String DB_URL = "db.url";
public static final String DB_DRIVER = "db.driver";
}
Then the constant class will be:
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;
}
Finally, use them:
// Constants int i = Constants.MYCONSTANT_ONE; // Properties String url = Constants.DB_URL;
I think this is a clean and elegant solution to fix constants and attributes in testing
