本文介绍了使用属性文件而不是静态最终变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有很多类,静态final字段用作默认值或配置。什么是最好的方法来创建全局配置文件?我应该将这些字段移动到单个静态类,使用属性文件或什么?
编辑:我需要在java类和xhtml页面中使用这个值。值不依赖于环境。我可以编译项目来设置新的值 - 没有问题。
解决方案
b
- 如果值根据运行时环境(例如数据库连接设置,外部服务器IP)而改变,或者很可能经常/很快改变,则在属性文件中放置东西
- 尽可能使用
enum
到static final
常量(避免stringly typed ) - 查找可能具有所需内容的现有库(例如,使用将小时转换为秒,而不是
static final int SECONDS_IN_HOUR = 3600; / code>)
- 剩下的是什么(希望不会很快改变),使用
public static final
- 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
tostatic 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 havingstatic 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
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 static class, use properties file or what?
Edit: I need to use this values both in java classes and xhtml pages. Values dosen't depend on envirnoment. I could compile project to set new values - no problem.
解决方案 The answer depends...
这篇关于使用属性文件而不是静态最终变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!