记录可用/所需 Java 属性文件内容的最佳实践
是否有一种完善的方法来记录 Java“属性”文件内容,包括:
- 指定给定密钥所需的数据类型/内容
- 指定应用程序运行是否需要密钥
- 提供密钥含义的描述
目前,我维护(手动)默认的 .properties 文件,并且我之前在注释中编写了数据类型的散文描述和每个键的描述。 这不会导致以编程方式访问属性文件。
我想我正在寻找的是属性文件的“getopt”等效项...
[编辑:相关]
Is there a well-established approach for documenting Java "properties" file contents, including:
- specifying the data type/contents expected for a given key
- specifying whether a key is required for the application to function
- providing a description of the key's meaning
Currently, I maintain (by hand) a .properties file that is the default, and I write a prose description of the data type and description of each key in a comment before. This does not lead to a programmatically accessible properties file.
I guess what I'm looking for is a "getopt" equivalent for properties files...
[EDIT: Related]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 Apache Commons Configuration 包中的一些功能。 它至少提供了对您的属性的类型访问。
传统的java属性文件中只有约定。 正如您所说,我见过的一些内容包括提供示例属性文件。 另一种方法是提供包含所有属性的默认配置,但将其注释掉。
如果您确实需要某些东西,也许您不是在寻找属性文件。 您可以使用 XML 配置文件并指定具有数据类型和要求的架构。 您可以使用 jaxb 将模式编译为 java 并以这种方式读取它。 通过验证,您可以确保所需的属性存在。
您所希望的最好结果是,当您执行应用程序时,它会读取、解析并验证文件中的属性。 如果您绝对必须保持基于属性并且不想使用 xml,但需要这种解析。 您可以有一个辅助属性文件,其中列出了可以包含的每个属性、其类型以及是否需要。 然后,您必须编写一个属性文件验证器,该验证器将接受要验证的文件以及类似验证模式的属性文件。 就像
我没有浏览所有 Apache 配置包,但它们通常有这样有用的实用程序。 如果您能在那里找到可以简化此操作的东西,我不会感到惊讶。
You could use some of the features in the Apache Commons Configuration package. It at least provides type access to your properties.
There are only conventions in the traditional java properties file. Some I've seen include providing, like you said, an example properties file. Another is to provide the default configuration with all the properties, but commented out.
If you really want to require something, maybe you're not looking for a properties file. You could use an XML configuration file and specify a schema with datatypes and requirements. You can use jaxb to compile the schema into java and read it i that way. With validation you can make sure the required properties are there.
The best you could hope for is when you execute your application, it reads, parses, and validates the properties in the file. If you absolutely had to stay properties based and didn't want to go xml, but needed this parsing. You could have a secondary properties file that listed each property that could be included, its type, and whether it was required. You'd then have to write a properties file validator that would take in a file to validate as well as a validation schema-like properties file. Something like
I haven't looked through all of the Apache Configuration package, but they often have useful utilities like this. I wouldn't be surprised if you could find something in there that would simplify this.
另一个值得关注的选项是名为 OWNER 的项目。 在那里,您可以使用类型和注释定义充当应用程序中的配置对象的接口。 然后,OWNER 查找并解析正确的
Properties
文件。 因此,您可以为您的界面编写一个 javadoc 并将其用作文档。Another option to check out is the project called OWNER. There, you define the interface that serves as the configuration object in your application, using types and annotations. Then, OWNER does the finding and parsing of the correct
Properties
file. Thus, you could write a javadoc for your interface and use that as the documentation.我从未见过这样做的标准方法。 我可能会做的是:
它不会给你带来任何比你手工做的事情更好的东西,除了你可以以不同的方式管理信息,这可能更容易处理——例如,你可以有一个程序来吐出注释来读入。它可能会为您提供所需的编程访问权限,但它是一种自行开发的东西。
或者可能只是工作太多而收获太少(这就是为什么没有明显的东西)。
如果你能指定你想看到的评论类型,如果我感到无聊,我可以尝试写一些东西:-)(这是我喜欢做的事情来取乐,我知道生病了:-)。
好吧...我很无聊...这至少是一个开始:-)
以及一个简单的测试:
I have never seen a standard way of doing it. What I would probably do is:
It doesn't get you anything over what you are doing by hand, except that you can manage the information in a different way that might be easier to deal with - for example you could have a program that spit out the comments to read in. It would potentially give you the programmatic access that you need, but it is a roll-your-own sort of thing.
Or it might just be too much work for too little to gain (which is why there isn't something obvious out there).
If you can specify the sort of comments you want to see I could take a stab at writing something if I get bored :-) (it is the sort of thing I like to do for fun, sick I know :-).
Ok... I got bored... here is something that is at least a start :-)
And a simple test for it:
一种简单的方法是使用示例属性文件分发您的项目,例如我的项目在 svn 中有一个“build.properties.example”,并根据需要注释了属性。 本地正确的属性不会进入 svn。
不过,既然你提到了“getopt”,我想知道你是否真的在考虑命令行参数? 如果有一个“main”需要特定的属性,我通常会将相关说明放在“useage”消息中,如果参数不正确或“-h”,该消息会打印出来。
One easy way is to distribute your project with a sample properties file, e.g. my project has in svn a "build.properties.example",with properties commented as necessary. The locally correct properties don't go into svn.
Since you mention "getopt", though, I'm wondering if you're really thinking of cmd line arguments? If there's a "main" that needs specific properties, I usually put it the relevant instructions in a "useage" message that prints out if the arguments are incorrect or "-h".