简体   繁体   English

如何记录.properties文件?

[英]How to document .properties files?

Is there a standard way of documenting .properties files? 有记录.properties文件的标准方法吗? Something in the spirit of Javadoc where I have properties and documentation in the same file and can leverage IDE integration? 我具有Javadoc精神,可以在同一文件中拥有属性和文档,并且可以利用IDE集成吗?

It might sound trivial but to illustrate my problem, consider the following example: Let's say I have a Java application which is controlled by a central .properties file in the format 这听起来很琐碎,但为说明我的问题,请考虑以下示例:假设我有一个Java应用程序,该应用程序由中央.properties文件控制,格式为

key1=value1
key2=value2

To document the meaning of property keys I could use comments like this: 为了记录属性键的含义,我可以使用如下注释:

# key1 does this and that
key1=value1

But what if I have settings that are applied more than once? 但是,如果我有多次应用的设置怎么办?

# key1 causes component1 to behave so and so
component1.key1=value1

# key1 causes component2 to behave so and so
component2.key1=value2

In the above, the meaning of key1 is the same in both properties. 在上面, key1在两个属性中的含义相同。 However, I don't want to repeat the comment because I might have hundreds of components to which the key1 property is applied. 但是,我不想重复注释,因为我可能有数百个将key1属性应用到的组件。 I could use a single comment at the beginning like this: 我可以像这样在开头使用单个注释:

# key1 causes the affected component to behave so and so
component1.key1=value1
component2.key1=value2
....

But then if I add a new component at the end, I would have to scroll up every time to find the associated documentation comment. 但是,如果最后添加一个新组件,则每次都必须向上滚动以找到相关的文档注释。

I'm looking for a way to define the meaning of the property pattern 我正在寻找一种定义属性模式含义的方法

[COMPONENT].key1=value1

in a way that I can easily refer to that documentation in an IDE - just like you do with Java code and Javadoc. 就像在Java代码和Javadoc中一样,我可以轻松地在IDE中引用该文档。

If your config is that complex I would consider using Typesafe config https://github.com/typesafehub/config/blob/master/README.md 如果您的配置是如此复杂,我会考虑使用Typesafe配置https://github.com/typesafehub/config/blob/master/README.md

Doing so would allow you to structure your config to remove duplication by using json or hcon. 这样做将允许您构造配置以使用json或hcon删除重复项。

Having done that, whatever loads the config should validate it and report any issues. 完成此操作后,无论加载了什么配置,都应对其进行验证并报告所有问题。 If successful, build an obect representation of this complex config. 如果成功,则构建此复杂配置的完整表示。 Javabean in Java, case classes in Scala. Java中的Javabean,Scala中的案例类。

Document the code that loads the config and the code representing the config. 记录加载配置的代码和代表配置的代码。

I've used this approach in Scala projects and it's saved a lot pain and confusion. 我在Scala项目中使用了这种方法,它避免了很多痛苦和混乱。

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

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