Use properties file instead of static final variables

怎甘沉沦 提交于 2019-12-03 06:05:14

The answer depends...

  • Put stuff in properties files if the values change depending on runtime environment (eg database connection settings, foreign server IPs), or that will likely change often/soon
  • Prefer using enum to static final constants where possible (avoid "stringly typed" code)
  • Find an existing library that might have what you want (eg use TimeUnit to convert hours to seconds, rather than having static final int SECONDS_IN_HOUR = 3600;)
  • What's left (that hopefully isn't going to change any time soon), use public static final in the class that has "most ownership" over them
  • Avoid classes that have static methods that return a constant - it's just code bloat

Both the approches are fine:

  1. Have a static class with required final fields.

  2. Have a singelton but save it from multiple threads appropriately.

  3. if possible use enum over static fields. This way you can group related fields together.

If these are application level values, i'll prefer static class over singelton.

and, you should decide if these are const values or configuration value that differ from time to time.

Y,es properties file is always preferable for configuration. There are multiple ways to read it, one being apache commons-configuration.

If the properties are environment-dependent, externalize them (outisde the project), and set the path to them (for example with -Dconfig.location=..). If they don't change depending on the environment, simply put the properties file on the classpath.

See this article of mine about the envioronment-dependent properties.

Then, you can have a static holder for the Properties / Configuration / ... object, or, if possible, inject (if using a DI framework) the values wherever they are needed.

As for me, the main difference between the approaches is an ability to change values without changing a code.

With static final fields you must recompile sources to use new values. With .properties files you just usually have to restart an application that may be done by system administrator etc.

So it seems to be a matter of whether it should be changeable or not and whether it should be available for someone except a developer. (In some sense it's a questions who is "in charge of" these values: developer or system administrator/user etc.)

My favorite approach is the follow:

public class MyProperties {
private static final String BUNDLE_NAME = "my.package.MyProperties"; //$NON-NLS-1$

private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle
        .getBundle(BUNDLE_NAME);


private MyProperties() {
}

public static long getLong(String key) {
    String strValue = getString(key);
    long result = -1;
    try {
        result = Long.parseLong(strValue);
    } catch (Exception exc) {
        logger.error(exc.getLocalizedMessage());
    }
    return result;
}

public static int getInteger(String key) {
    String strValue = getString(key);
    int result = -1;
    try {
        result = Integer.parseInt(strValue);
    } catch (Exception exc) {
        logger.error(exc.getLocalizedMessage());
    }
    return result;
}

public static String getString(String key) {
    String returnValue = System.getProperty(key);
    if(returnValue != null && returnValue.length() > 0) {
        if(logger.isDebugEnabled()) {
            logger.debug(key+" assigned by system property");
        }
        return returnValue;
    }
    try {
        returnValue = RESOURCE_BUNDLE.getString(key);
    } catch (MissingResourceException e) {
        returnValue = '!' + key + '!';
    }
    return returnValue;
}
}

This simple class first search the key in the system properties then in the resource bundle. This means you can override the property settings with -Dkey=value command line options.

It is good to have them on one place. Property file or static class? Property file should be used for e.g. localization. Static class for e.g. String constants.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!