JAXB MOXy 中的静态工厂方法
我定义一个静态工厂方法:
@XmlType(factoryClass=DummyFactory.class, factoryMethod="createNew")
public abstract MyClass() {
}
我使用工厂方法的原因是 MyClass 是抽象的,如何获取它的实例取决于类的某些注释。此逻辑嵌入在工厂方法 createNew
中。
另外,工厂类DummyFactory
是抽象的。据我了解,如果工厂方法是静态的,工厂类不需要提供默认构造函数(http://download.oracle.com/javaee/6/api/javax/xml/bind/annotation/XmlType.html)。
这是工厂类的原始简化:
public abstract class DummyFactory {
public static MyClass createNew() {
// code for returning a new instance of MyClass
}
}
但是,当我尝试解组 XML 文档时,我收到以下异常:
Exception [EclipseLink-171] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The factory class does not define a public default constructor, or the constructor raised an exception.
Internal Exception: java.lang.InstantiationException
首先,我认为我在工厂类和方法的解释中没有得到正确的信息,但后来我尝试使用 JAXB RI,效果很好。 所以我的问题是:
有没有办法让 MOXy 与抽象工厂类一起工作?
(JAXB RI 给我带来了其他类型的问题,这就是我不想使用它的原因)。
I am defining a static factory method with:
@XmlType(factoryClass=DummyFactory.class, factoryMethod="createNew")
public abstract MyClass() {
}
The reason I am using factory methods is that MyClass
is abstract, and how to obtain an instance of it depends on certain annotations of the class. This logic is embedded in the factory method createNew
.
In addition, the factory class DummyFactory
is abstract. As far as I understand factory classes do not need to provide a default constructor if their factory method is static (http://download.oracle.com/javaee/6/api/javax/xml/bind/annotation/XmlType.html).
This is a raw simplification of how the factory class looks like:
public abstract class DummyFactory {
public static MyClass createNew() {
// code for returning a new instance of MyClass
}
}
However, when I try to unmarshal a XML document, I am getting the following exception:
Exception [EclipseLink-171] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The factory class does not define a public default constructor, or the constructor raised an exception.
Internal Exception: java.lang.InstantiationException
First I thought that I did not get something correctly in the explanation of factory classes and methods, but then I tried with JAXB RI and this is working fine there.
So my question is:
Is there a way to make MOXy work with abstract factory classes ?
(JAXB RI is giving me other kind of problems, that is the reason I would prefer not using it).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢您为此问题输入错误 (https://bugs.eclipse.org/362984)。该问题已在 EclipseLink 2.4 流中得到修复,并将于今天(2011 年 11 月 9 日)向后移植到 EclipseLink 2.3.2 流中。您可以尝试从以下位置获取每晚下载的修复:
现在,如果您使用
@XmlType
注释指定工厂类,例如:支持以下类型的工厂类:
具有静态方法的工厂< /strong>
通过此错误修复,当 MOXy使用工厂方法创建
MyClass
实例,但未创建DummyFactory
实例。具有实例方法的工厂
除了静态方法之外,MOXy 还允许您指定实例级创建方法。对于这些方法,MOXy 将创建工厂类的实例。
JAXB RI 中不允许此配置,您将收到以下异常:
Thank you for entering a bug for this issue (https://bugs.eclipse.org/362984). The issue has been fixed in the EclipseLink 2.4 stream, and will be backported today (Nov 9, 2011) to the EclipseLink 2.3.2 stream. You can try out the fix obtaining a nightly download from:
Now if you specify a factory class using the
@XmlType
annotation like:The following types of factory classes are supported:
Factory with Static Methods
With this bug fix, when MOXy uses the factory method to create an instance of
MyClass
an instance ofDummyFactory
is not created.Factory with Instance Methods
In addition to static methods, MOXy allows you to specify instance level creation methods. For these methods MOXy will create an instance of the factory class.
This configuration is not allowed in the JAXB RI, and you will get the following exception: