java 注释:用 xml 文件覆盖注释的库
Java 有注释,这很好。然而,一些开发人员认为最好使用 xml 文件用元数据注释代码 - 其他开发人员更喜欢注释,但会使用元数据覆盖源代码中的注释。
我正在编写一个使用注释的 Java 框架。问题是:是否有一种标准方法来定义和解析 xml 文件中的元数据。我认为这是每个使用注释的框架都可以受益的东西,但我似乎可以在互联网上找到类似的东西。
我必须自己进行 xml 解析/验证吗?或者有人已经做过类似的事情了吗?
Java has annotations and that is good. However, some developers feel that it is best to annotate code with metadata using xml files - others prefer annotations but would use metadata to override annotations in source code.
I am writing a Java framework that uses annotations. The question is: is there a standard way to define and parse metadata from xml files. I think this is something every framework that uses annotations could benefit from but I can seem to find something like this on the Internet.
Must I roll my own xml parsing/validation or has someone already done something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有标准的方法,但这里有一些 Java 框架可以做到这一点:
There is not a standard way, but here are some Java frameworks who does it:
我编写了 Annox 库,它正是您所需要的。使用 Annox,您可以从 XML 读取任意注释。
I wrote the Annox library which does exactly what you need. With Annox you can read arbitrary annotations from XML.
这并不完全是您想要的,但是 backport175 项目 具有针对 Java 5 之前的 Java 版本的注释实现。
它具有您搜索的一些功能,它将读取自己的样式实现和“真实”注释(如果存在)。也许这可以作为构建更通用框架的起点。
It's not exactly what you want, but the backport175 project has an implementation of annotations for Java versions before Java 5.
It has some of the functionality you search in that it will read both its own style implementations and "real" annotations if they are present. Maybe this can be used as a starting point to build a more general framework.
使用 JAXB http://java.sun.com/developer/technicalArticles/WebServices/jaxb /
您将为元数据文件编写 xsd,生成可以帮助您解析 xml 文件的 JAXB 类。
Use JAXB http://java.sun.com/developer/technicalArticles/WebServices/jaxb/
You would write the xsd for your metadata file, generate JAXB classes that can help you parse the xml files.