Java 中的属性文件与常量类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10801746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:38:22  来源:igfitidea点击:

Properties file vs Constants class in Java

java

提问by Anand

I am a bit confused between using a constants file and properties file in Java.

我对在 Java 中使用常量文件和属性文件有点困惑。

How to decide when to use Constants.javaand when to use a .propertiesfile?

如何决定何时使用Constants.java以及何时使用.properties文件?

回答by Stephen C

Use hardwired constants in your Java code when you don't wantusers / deployers / testers / tests changing them.

您不希望用户/部署者/测试者/测试改变它们时,在您的 Java 代码中使用硬连线常量。

Use a properties file when you do wantthis 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. You need to understand your own application requirements and decide for yourself.

不可能将其简化为“总是喜欢 X”的建议。您需要了解自己的申请要求并自行决定。

回答by techuser soma

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". Not really a key value. Keys for cache etc.

对于像 YES = "yes" 这样的常量。不是真正的关键值。缓存等的键

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.get(KeyConstants.SOME_KEY) from different classes, ofcouse xyz being shared or singleton.

即使来自应用程序中的不同位置,用于确保检索和设置使用相同键的常量,例如 xyz.put(KeyConstants.SOME_KEY, "somevalue"); xyz.get(KeyConstants.SOME_KEY) 来自不同的类,当然 xyz 是共享的或单例的。

回答by adarshr

  • 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.

  • 常量- 当您不介意每次更改值时重新编译应用程序时。这里有点讽刺。如果它被称为常量,你为什么要改变它:)

  • 属性文件- 当您想要更改值并可能重新启动应用程序以获取更改时。

回答by Peter

Generally, anything in a constants class is considered to be "hardcoded"; that is, you are required to re-compile to make changes.

通常,常量类中的任何内容都被认为是“硬编码”的;也就是说,您需要重新编译才能进行更改。

Use a .propertiesfile for things like configuration, where you don't have to be forced to re-compile just to make changes. In this way, you can simply change the properties file and restart your application.

.properties文件用于配置之类的事情,您不必为了进行更改而被迫重新编译。通过这种方式,您可以简单地更改属性文件并重新启动您的应用程序。

回答by ftr

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.

当参数不是动态的,因此不应根据外部因素进行更改时,您可以使用常量。每次更改值时都必须重新编译该类。

回答by javatutorial

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.

另一方面,常量类需要编译,但如果您不打算经常更改这些常量,则它们“更安全”。

回答by fastcodejava

Constants are fixed in compile time. So if you don't foresee any changes of values Constants will be good idea.

常量在编译时是固定的。因此,如果您没有预见到任何值的变化,常量将是个好主意。

回答by SantoshK

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. You might end up using both, though.

所以答案是,这取决于。如果属性中的值确实是您想要外部化的配置更改,则使用属性文件。否则使它们成为 Java 常量。不过,您最终可能会同时使用两者。