Use properties file instead of static final variables

后端 未结 6 1498
轮回少年
轮回少年 2021-02-08 03:05

I have many classes with static final fields which are used as default values or config. What is best approach to create global config file? Should I move these fields to single

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-08 03:25

    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.

提交回复
热议问题