JiBX:如何在代码中继续使用接口?

时间:2021-08-05 21:50:20

How can I keep my using interfaces in classes I want to use JiBX binding with?

如何在我想使用JiBX绑定的类中保留我的使用接口?

Example: I have this very simple model in java:

示例:我在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

java.lang.ClassFormatError:类com / test / B中的方法具有非法修饰符:0x1001

I've tried to use 'abstract="true"' on both mapping, only to get this exception:

我试图在两个映射上使用'abstract =“true”',只是为了得到这个异常:

...Caused by: org.jibx.runtime.JiBXException: Unable to access binding information for class com.test.A Make sure the binding has been compiled...

...引起:org.jibx.runtime.JiBXException:无法访问类com.test.A的绑定信息确保绑定已编译...

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.

我发现的唯一解决方案是让AImpl持有BImpl而不是B,并让getter返回BImpl并且setter接收BImpl。这是非常错误的,因为它完全破坏了界面。

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.

我怎么解决这个问题?我一直在拔毛,发脾气(真正的问题要复杂得多,并且JiBX神秘的错误信息没有帮助) - 没有任何帮助。

Is this solvable? Is JiBX really that intrusive (in that it requires me to abandon all interface programming?)

这可以解决吗? JiBX是否真的具有侵入性(因为它要求我放弃所有接口编程?)

Please don't answer "use AbstractB" as it's the same problem, only one level removed.

请不要回答“使用AbstractB”,因为它是同一个问题,只删除了一个级别。

2 个解决方案

#1


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:

在映射中,您应该能够使用“create-type”属性来指定JiBX应该为具有接口类型的bean属性实例化的具体类。我对收集属性使用了很多。例如,您可以告诉JiBX为java.util.Set类型的属性实例化java.util.HashSet。但我相信它对非收集属性也同样有效。您的映射看起来像:

<mapping class="com.mypackage.AImpl" name="A">
  <structure get-method="getB" set-method="setB" create-type="com.mypackage.BImpl">
    ...
  </structure>
  ...
</mapping>

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.

JiBX将调用no-arg构造函数来创建B对象。或者,如果需要花哨的实例化逻辑,可以使用工厂或自定义序列化器/反序列化器。有关详细信息,请参阅此参考页

#2


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:

另一个很好的资源是binding.dtd - 显然它不在发行版中,但可以从这里下载:http://jibx.cvs.sourceforge.net/viewvc/checkout/jibx/core/docs/binding.dtd。将此文件放在某处(例如c:\ binding.dtd)。然后,在顶部绑定条目中,使用:

<binding xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file://jibx/binding.dtd">

and register file://jibx/binding.dtd to point to your saved binding.dtd for documentation and verification goodies.

并注册file://jibx/binding.dtd以指向您保存的binding.dtd以获取文档和验证信息。

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.

惯性是多么令人惊奇 - 我知道xml文件应该有架构/ dtds,我之前使用过它们并且总是说“没有架构理解这将是不可能的”。然而,当我进入这个项目时,我从来没有想过为这个xml搜索schema / dtd - 我只是接受它,因为它没有。学过的知识。

#1


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:

在映射中,您应该能够使用“create-type”属性来指定JiBX应该为具有接口类型的bean属性实例化的具体类。我对收集属性使用了很多。例如,您可以告诉JiBX为java.util.Set类型的属性实例化java.util.HashSet。但我相信它对非收集属性也同样有效。您的映射看起来像:

<mapping class="com.mypackage.AImpl" name="A">
  <structure get-method="getB" set-method="setB" create-type="com.mypackage.BImpl">
    ...
  </structure>
  ...
</mapping>

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.

JiBX将调用no-arg构造函数来创建B对象。或者,如果需要花哨的实例化逻辑,可以使用工厂或自定义序列化器/反序列化器。有关详细信息,请参阅此参考页

#2


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:

另一个很好的资源是binding.dtd - 显然它不在发行版中,但可以从这里下载:http://jibx.cvs.sourceforge.net/viewvc/checkout/jibx/core/docs/binding.dtd。将此文件放在某处(例如c:\ binding.dtd)。然后,在顶部绑定条目中,使用:

<binding xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file://jibx/binding.dtd">

and register file://jibx/binding.dtd to point to your saved binding.dtd for documentation and verification goodies.

并注册file://jibx/binding.dtd以指向您保存的binding.dtd以获取文档和验证信息。

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.

惯性是多么令人惊奇 - 我知道xml文件应该有架构/ dtds,我之前使用过它们并且总是说“没有架构理解这将是不可能的”。然而,当我进入这个项目时,我从来没有想过为这个xml搜索schema / dtd - 我只是接受它,因为它没有。学过的知识。