简体   繁体   English

Java 中的属性文件与常量类

[英]Properties file vs Constants class in Java

I am a bit confused between using a constants file and properties file in Java.我对在 Java 中使用常量文件和属性文件有点困惑。

How to decide when to use Constants.java and when to use a .properties file?如何决定何时使用Constants.java以及何时使用.properties文件?

Use hardwired constants in your Java code when you don't want users / deployers / testers / tests changing them.您不希望用户/部署者/测试者/测试改变它们时,在您的 Java 代码中使用硬连线常量。

Use a properties file when you do want this to be a possibility.您确实希望这成为可能时,使用属性文件。

The point is that changing a hard-wired constant in your application's source code entails editing the source code, rebuilding and redeploying.关键是更改应用程序源代码中的硬接线常量需要编辑源代码、重新构建和重新部署。 By contrast, changing a properties file may be as simple as firing up NotePad.相比之下,更改属性文件可能就像启动 NotePad 一样简单。


You commented:你评论说:

As you said that changing properties file is simple whereas changing constants file requires us to rebuild the application.正如您所说,更改属性文件很简单,而更改常量文件需要我们重新构建应用程序。 So, shouldn't we always prefer to use properties file?那么,我们不应该总是更喜欢使用属性文件吗?

No. Not always.不,并非总是如此。 For example , if you distribute your application to end users to install on their machines and it has constants that you do not want users to change it would be a bad idea to put them in a properties file.例如,如果您将应用程序分发给最终用户以安装在他们的机器上,并且您不希望用户更改它的常量,那么将它们放在属性文件中将是一个坏主意。

It is impossible to reduce this to "always prefer X" recommendation.不可能将其简化为“总是喜欢 X”的建议。 You need to understand your own application requirements and decide for yourself.您需要了解自己的应用要求并自行决定。

My check list我的检查清单

Property file:属性文件:

Is it configurable per environemnt etc.是否可以根据环境等进行配置?

Messages, labels etc.消息、标签等

Applicable to particular situation (list of states for rules etc).适用于特定情况(规则等的状态列表)。 key-value pairs.键值对。 Can be modified by someone other than developer ie, analysts, business users etc.可由开发人员以外的其他人(即分析师、业务用户等)修改。

Constant:持续的:

Are constants.是常数。 Not configurable.不可配置。 Mostly for optimization and reuse.主要用于优化和重用。 To avoid keys being scattered.以免钥匙散落。

For constants like YES = "yes".对于像 YES = "yes" 这样的常量。 Not really a key value.不是真正的关键值。 Keys for cache etc.缓存等的键

Constants to ensure that retrieval and set use the same key even though from different places in the application, EXAMPLE xyz.put(KeyConstants.SOME_KEY, "somevalue");即使来自应用程序中的不同位置,用于确保检索和设置使用相同键的常量,例如 xyz.put(KeyConstants.SOME_KEY, "somevalue"); xyz.get(KeyConstants.SOME_KEY) from different classes, ofcouse xyz being shared or singleton. xyz.get(KeyConstants.SOME_KEY) 来自不同的类,当然 xyz 是共享的或单例的。

  • Constants - when you don't mind re-compiling the application each time you change value.常量- 当您不介意每次更改值时重新编译应用程序时。 Sort of an irony here.这里有点讽刺。 Why would you change something if it has been called a constant :)如果它被称为常量,你为什么要改变它:)

  • Properties file - when you want the luxury of just changing the value and maybe restarting the application to pick up the change.属性文件- 当您想要更改值并可能重新启动应用程序以获取更改时。

Generally, anything in a constants class is considered to be "hardcoded";通常,常量类中的任何内容都被认为是“硬编码”的; that is, you are required to re-compile to make changes.也就是说,您需要重新编译才能进行更改。

Use a .properties file for things like configuration, where you don't have to be forced to re-compile just to make changes..properties文件用于诸如配置之类的事情,您不必为了进行更改而被迫重新编译。 In this way, you can simply change the properties file and restart your application.通过这种方式,您可以简单地更改属性文件并重新启动您的应用程序。

You usually use property files when you want to specify parameters that vary from deployment to deployment or over time.当您想要指定因部署而异或随时间变化的参数时,通常会使用属性文件。

You use constants when the parameters are not dynamic and thus not supposed to be changed depending on external factors.当参数不是动态的,因此不应根据外部因素进行更改时,您可以使用常量。 You have to recompile the class every time you change a value.每次更改值时都必须重新编译该类。

There are many different aspects to consider.有许多不同的方面需要考虑。 The most important are that最重要的是

  • you can edit properties files and use it without re-compiling your application;您可以编辑属性文件并使用它,而无需重新编译您的应用程序;
  • properties files contain no code, so everybody can change them without any Java knowledge. properties 文件不包含任何代码,因此每个人都可以在没有任何 Java 知识的情况下更改它们。

These aspects make properties file really useful for storing configurations.这些方面使属性文件对于存储配置非常有用。

On the other hand, constants classes need to be compiled, but are "safer" if you plan not to change those constants very often.另一方面,常量类需要编译,但如果您不打算经常更改这些常量,则它们“更安全”。

Constants are fixed in compile time.常量在编译时是固定的。 So if you don't foresee any changes of values Constants will be good idea.因此,如果您没有预见到任何值的变化,常量将是个好主意。

Properties have the advantage of being externalizable.属性具有可外部化的优点。 You can have one property file for development, another for test and another for production.您可以有一个用于开发的属性文件,另一个用于测试,另一个用于生产。 A lot of times, though, rarely changed properties, that are internal to the workings of the software are put into properties file making them hard to manage.但是,很多时候,软件工作内部的很少更改的属性被放入属性文件中,使它们难以管理。

OTOH constants are compiled so any errors will be caught at compile time. OTOH 常量被编译,因此任何错误都将在编译时被捕获。

So the answer is , it depends.所以答案是,这取决于。 If the values in the properties are truly configuration changes you want to externalize then use properties file.如果属性中的值确实是您想要外部化的配置更改,则使用属性文件。 else make them Java constants.否则使它们成为 Java 常量。 You might end up using both, though.不过,您最终可能会同时使用两者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM