我用XmlJavaTypeAdapters注释了什么package-info? [英] What package-info do I annotate with XmlJavaTypeAdapters?

查看:137
本文介绍了我用XmlJavaTypeAdapters注释了什么package-info?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我研究过 Blaise Doughan对这个问题的回答,但还有一个问题。

I've studied Blaise Doughan's answer to a question on this subject but have a further question.

XmlJavaTypeAdapters 允许您列出一堆 XmlJavaTypeAdapter 注释,每个注释都管理JAXB如何将不可绑定类型映射到可绑定类型。

XmlJavaTypeAdapters lets you list a bunch of XmlJavaTypeAdapter annotations, each of which governs how a non-bindable type is mapped to a bindable type by JAXB.

您可以在包级别使用此批注。执行此操作时,每个 XmlJavaTypeAdapter 注释都需要完全指定其 type()属性。

You can use this annotation at the package level. When you do so, every XmlJavaTypeAdapter annotation needs its type() attribute fully specified.

似乎没有要求正在注释的包与要调整的不可绑定类型的包有任何关系。这很方便也很好。

There does not appear to be a requirement that the package that is being annotated have anything to do with the package of the non-bindable types being adapted. That is convenient and nice.

然而,这引出了我的下一个问题:如果注释包与被调整类型的包之间没有关系, JAXB如何发现包级别 XmlJavaTypeAdapters 注释?换句话说,它是如何知道哪些包可以参考潜在的 XmlJavaTypeAdapters 注释?我可以在我的 .ear 文件 .jar 文件中随机打包> lib 包含单个ginormous package-info 类的目录,该类使用所有不可绑定类型的所有适配器进行注释?

That, however, leads me to my next question: if there is no relationship between the annotated package and the package of the type being adapted, how does JAXB discover package-level XmlJavaTypeAdapters annotations? How, in other words, does it know which packages to consult for potential XmlJavaTypeAdapters annotations? May I make a random package in, say, a .jar file in my .ear file's lib directory that contains a single, ginormous package-info class that is annotated with all the adapters for all of my non-bindable types?

推荐答案

当JAXB运行时加载带有JAXB注释的类时,它会查找 package-info.java 与该类在同一个包中,并检查是否要查找包级注释。因此,虽然 XmlJavaTypeAdapters 不必与不可绑定类型驻留在同一个包中,但 必须驻留在同一个包中package作为可绑定类型。

When the JAXB runtime loads a JAXB-annotated class, it looks for a package-info.java in the same package as that class, and checks that to look for package-level annotations. So while XmlJavaTypeAdapters doesn't have to reside in the same package as the "non-bindable" types, it does have to reside in the same package as the "bindable" types.

例如,假设我有一个带有JAXB注释的类 A ,包 X ,包 B ,包 Y 。为了绑定 B 属性,假设需要一个类型适配器。该适配器可以在 A 本身中指定,也可以在包<中的 package-info.java 中指定。 code> X 。包 Y 非常随意,对JAXB运行时没用。

For example, say I have a JAXB-annotated class A, in package X, which has a property of type B in package Y. In order to bind the B property, let's say a type adapter is required. That adapter can be specified in A itself, or it can be specified in the package-info.java in package X. Package Y is pretty much arbitrary, and is of no interest to the JAXB runtime.

我希望这是有意义的。

这篇关于我用XmlJavaTypeAdapters注释了什么package-info?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆