String Constants Vs Resource Bundle in an Java web Application

牧云@^-^@ 提交于 2020-01-23 02:31:07

问题


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

  1. Is there any performance hit if I use the properties file compared to Static String Constant?
  2. Which is better Static string constant and Properties File Key Value pair?
  3. Is that good idea id we use the static String Constant(For Labeling) in the jsp?

Please suggest me


回答1:


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.




回答2:


I would prefer resource bundles in this case:

  1. Your code is far more likely to have performance issues that resource bundles. Don't throw away a perfectly good technology without data to justify it.
  2. Resource bundles will allow you to internationalize your app easily; not so with static strings.
  3. It's a Java idiom that's easily implemented in JSPs if you're already using JSTL (and you should be).


来源:https://stackoverflow.com/questions/2305727/string-constants-vs-resource-bundle-in-an-java-web-application

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