Java Web 应用程序中的字符串常量与资源包
从去年开始,我们开发了一个应用程序,在其中我们使用静态字符串常量来存储常量。 喜欢 public static final String PAYMENT_CHEQUE = "支票"; 在我需要的地方,即在 jsp 页面以及 Action 类中,我将引用上面的字符串常量 我在查看资源包、属性文件后想,我的问题是,
- 如果我 使用属性文件进行比较 静态字符串常量?
- 静态字符串哪个更好 常量和属性文件密钥 值对?
- 这是个好主意吗?我们使用 静态字符串常量(用于标签) 在jsp中?
请推荐我
From past Year are so, we have developed an application, in which we have used the static String Constants to store the Constants.
Like
public static final String PAYMENT_CHEQUE = "cheque";
Where Ever I Require i.e. in the jsp pages as well the Action class I will be refering to the Above String Constant
I am thinking after reviewing the Resource Bundle, properties Files, My Question is
- Is there any performance hit if I
use the properties file compared to
Static String Constant? - Which is better Static string
constant and Properties File Key
Value pair? - Is that good idea id we use the
static String Constant(For Labeling)
in the jsp?
Please suggest me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,使用硬编码字符串是一个坏主意,因为这意味着每次更改都需要一个新的编译部署周期,而使用属性文件意味着应用程序需要重新启动,而不需要其他任何东西(尽管有些人会说这本身就是付出的代价很高)。使用属性文件的另一个优点是能够通过将应用程序切换到另一个文件来切换语言 - 比在代码中拥有多组常量标签更合理。
您可以像现在一样使用最终的静态字符串字段,只在应用程序的初始化阶段为它们提供从属性文件读取的值,而不是返回每个常量的文件(这将是昂贵的!)。这样您就不必进行应用程序范围的更改,并且您仍然可以享受我上面提到的优势。
In general, using hard-coded strings is a bad idea, because it means every change needs a new compilation-deployment cycle, whereas using a properties file means the application needs to be restarted and nothing else (although some would say that in itself is a high price to pay). Another advantage of using a properties file is the ability to switch languages by switching the application to another file - much more reasonable than having multiple sets of constant labels in code.
You can use the final static String fields the same way you do right now, only give them a value read fro ma properties file at the application's initialization stage, rather than going back to the file for every constant (which would be costly!). That way you don't have to make application-wide changes, and you still enjoy the advantages I mentioned above.
在这种情况下,我更喜欢资源包:
I would prefer resource bundles in this case: