JiBX:如何在代码中继续使用接口?
如何在我想要使用 JiBX 绑定的类中保留使用接口?
例子: 我在java中有这个非常简单的模型:
public interface A {
B getB();
void setB(B b);
}
public interface B {
String getData();
void setData(String data);
}
public class AImpl implements A {
B b;
@Override
public B getB() {
return b;
}
@Override
public void setB(B b) {
this.b = b;
}
}
public class BImpl implements B {
private String data;
@Override
public String getData() {
return data;
}
@Override
public void setData(String data) {
this.data = data;
}
}
和这个绑定文档:
<binding>
<mapping name="A"
class="com.test.AImpl">
<structure name="B" usage="optional" get-method="getB" set-method="setB"/>
</mapping>
<mapping name="B"
class="com.test.BImpl">
<value name="data" set-method="setData" get-method="getData" usage="optional"/>
</mapping>
</binding>
当我尝试运行我的代码时,我得到这个异常:
java.lang.ClassFormatError:方法 在 com/test/B 类中存在非法 修饰符:0x1001
我尝试在两个映射上使用 'abstract="true"',只是为了得到此异常:
...造成的原因: org.jibx.runtime.JiBXException:无法 访问绑定信息 class com.test.A 确保绑定 已编译...
我发现的唯一解决方案是让 AImpl 保存 BImpl 而不是 B,并让 getter 返回 BImpl 和 setter 接收 BImpl。 这是非常错误的,因为它完全破坏了界面。
我该如何解决这个问题? 我一直在发脾气,发脾气(真正的问题要复杂得多,JiBX 神秘的错误消息没有帮助)——没有任何帮助。
这个可以解决吗? JiBX 真的那么具有侵入性吗(因为它要求我放弃所有接口编程?)
请不要回答“使用 AbstractB”,因为这是同样的问题,只是删除了一级。
How can I keep my using interfaces in classes I want to use JiBX binding with?
Example:
I have this very simple model in java:
public interface A {
B getB();
void setB(B b);
}
public interface B {
String getData();
void setData(String data);
}
public class AImpl implements A {
B b;
@Override
public B getB() {
return b;
}
@Override
public void setB(B b) {
this.b = b;
}
}
public class BImpl implements B {
private String data;
@Override
public String getData() {
return data;
}
@Override
public void setData(String data) {
this.data = data;
}
}
And this binding document:
<binding>
<mapping name="A"
class="com.test.AImpl">
<structure name="B" usage="optional" get-method="getB" set-method="setB"/>
</mapping>
<mapping name="B"
class="com.test.BImpl">
<value name="data" set-method="setData" get-method="getData" usage="optional"/>
</mapping>
</binding>
When I try to run my code I get this exception:
java.lang.ClassFormatError: Method
in class com/test/B has illegal
modifiers: 0x1001
I've tried to use 'abstract="true"' on both mapping, only to get this exception:
...Caused by:
org.jibx.runtime.JiBXException: Unable
to access binding information for
class com.test.A Make sure the binding
has been compiled...
The only solution I've found is to have AImpl hold a BImpl instead of a B, and have the getter return BImpl and the setter recieve BImpl. This is very wrong as it breaks the interface completely.
How can I solve this? I've been pulling hairs out, having tantrums (the real issue is much more complex, and JiBX cryptic error messages don't help) - nothing help.
Is this solvable? Is JiBX really that intrusive (in that it requires me to abandon all interface programming?)
Please don't answer "use AbstractB" as it's the same problem, only one level removed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在映射中,您应该能够使用“create-type”属性来指定 JiBX 应该为具有接口类型的 bean 属性实例化的具体类。 我经常将其用于集合属性。 例如,您可以告诉 JiBX 为 java.util.Set 类型的属性实例化一个 java.util.HashSet。 但我相信它对于非收藏属性也同样有效。 您的映射将类似于:
JiBX 将调用无参数构造函数来创建 B 对象。 或者,如果您需要奇特的实例化逻辑,您可以使用工厂或自定义序列化器/反序列化器。 有关详细信息,请参阅此参考页。
In the mapping, you should be able use the "create-type" attribute to specify the concrete class that JiBX should instantiate for bean properties that have an interface type. I use this a lot for collection properties. For example, you can tell JiBX to instantiate a java.util.HashSet for a property of type java.util.Set. But I believe it works just as well for non-collection properties. Your mapping would look something like:
JiBX will call the no-arg constructor to create the B object. Alternatively, you could use a factory or a custom serializer/deserializer if you need fancy instantiation logic. See this reference page for details.
另一个好的资源是 binding.dtd - 显然它不在发行版中,但可以从这里下载: http://jibx.cvs.sourceforge.net/viewvc/checkout/jibx/core/docs/binding.dtd。 将此文件放在某处(例如 c:\binding.dtd)。 然后,在顶部绑定条目中,使用以下命令:
并注册 file://jibx/binding.dtd 以指向您保存的 binding.dtd 以获取文档和验证内容。
惯性的作用是令人惊奇的 - 我知道 xml 文件应该有模式 / dtds,我以前使用过它们并且总是说“如果没有模式理解,这是不可能的”。 然而,当我进入这个项目时,我从来没有想过要搜索这个 xml 的模式/dtd - 我只是接受它,因为它没有。
学过的知识。
Another good resource is the binding.dtd - apparently it's not in the distribution but can be downloaded from here: http://jibx.cvs.sourceforge.net/viewvc/checkout/jibx/core/docs/binding.dtd. Put this file somewhere (c:\binding.dtd for example). Then, in the top binding entry, use this:
and register file://jibx/binding.dtd to point to your saved binding.dtd for documentation and verification goodies.
It's amazing what inertia does - I know that xml files should have schemas / dtds, I've used them before and always said "without a schema understanding this would've been impossible". Yet when I've entered this project, it never occurred to me to search for the schema / dtd for this xml - I just accepted it as given that it had none.
Lesson learned.