Java .properties 文件的命名约定是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14941004/
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 17:57:35  来源:igfitidea点击:

What are the naming conventions for Java .properties files?

javapropertiesnaming-conventions

提问by Radan

I plan to create a global propertiesfile for use across the application, and several properties files available only for specific classes and methods. Is there any particular industry standard for naming the property files?

我计划创建一个在整个应用程序中使用的全局属性文件,以及几个仅可用于特定类和方法的属性文件。是否有任何特定的行业标准来命名属性文件?

采纳答案by JoshDM

To my knowledge, there is no set rule.

据我所知,没有固定的规则。

The naming convention I go by is if it is a properties for a single class, I use {ClassName}.properties, otherwise I use {WhatIsItUsedFor}.properties, and occasionally if it's for a single application, {ApplicationName}.properties. I have a preference for CamelCase; others prefer lowercase.

我遵循的命名约定是,如果它是单个类的属性,我使用{ClassName}.properties,否则我使用{WhatIsItUsedFor}.properties,偶尔如果它是单个应用程序的,{ApplicationName}.properties. 我偏爱 CamelCase;其他人更喜欢小写。

For the names of properties themselves, if granularity is possible, I use something like

对于属性本身的名称,如果粒度是可能的,我使用类似

{ClassName}.{MethodNameIfNeeded}.{IntendedVariable}={value}

Do realize the more property files you have, the more potential maintenance issues you create. Some are better off consolidating to a single properties file, using the property/value naming convention (above) to single-out any classes requiring their own configuration.

请务必意识到您拥有的属性文件越多,您创建的潜在维护问题就越多。有些最好合并到单个属性文件中,使用属性/值命名约定(上面)来挑出任何需要自己配置的类。

回答by David Roussel

Short answer: no

简短回答:没有

Typically you'd name the global properties 'myapp.properties'. Have a look around at some confgiration frameworks, see if they do what you want. Such as commons-configuration.

通常,您将全局属性命名为“myapp.properties”。看看一些配置框架,看看它们是否能满足您的需求。例如commons-configuration

回答by Dave Newton

No; name them something meaningful to the context in which they'll be used.

不; 为它们命名对它们将被使用的上下文有意义的东西。

You'll see things like "applicationResources.properties", "messages.properties", etc. right alongside filenames that have more business-specific meanings, like "account.properties".

您会看到诸如“applicationResources.properties”、“messages.properties”等内容,旁边还有具有更多业务特定含义的文件名,例如“account.properties”。

回答by Honza Zidek

The naming is more complex if you consider also localization. See Java doc for java.util.ResourceBundle:

如果您还考虑本地化,命名会更加复杂。请参阅Java 文档java.util.ResourceBundle

baseName + "_" + language + "_" + script + "_" + country + "_" + variant

基本名称 + "_" + 语言 + "_" + 脚本 + "_" + 国家 + "_" + 变体

Examples:

例子:

MyResource_fr_FR
MyResource_fr_CA
MyResource_fr_CH
MyResource_en_Latn_US_WINDOWS_VISTA
MyResource_en_Latn_US_WINDOWS
MyResource_en_Latn_US
MyResource_en_Latn
MyResource_en_US_WINDOWS_VISTA
MyResource_en_US_WINDOWS
MyResource_en_US
MyResource_en

回答by ty733420

Not really. Name the file with a .properties extension (I've also seen .prp).

并不真地。使用 .properties 扩展名命名文件(我也见过 .prp)。

I would recommend against using a "global" properties file, and instead maintain like configurations in segregated files, i.e.:

我建议不要使用“全局”属性文件,而是在隔离文件中维护类似的配置,即:

database.properties
smtp.properties
messages.properties