繁体   English   中英

用JAXB注释应用Composite模式的最佳方法?

[英]Best way to apply Composite pattern with JAXB annotation?

考虑使用JAXB构建此xml消息:

<msg>
  <database id="111">
     <table name="t1" pkCol="id">
        <row op="update">
            <col name="id" value="12345"/>
            <col name="age" value="30"/>
            <col name="name" value="John"/>
        </row>
        <row ...>
        </row>
     </table>
     <table ...>
       :
     </table>
   </database>
   <database ...>
     <table ...>
     </table>
   </database>
</msg>

我们现有的旧版实现为每个标签都有一个单独的类,即消息,数据库,表,行和列。 没有共同的基类。 每个类都有一个List / ArrayList,一个无参数构造函数以及用于@XmlAttribute和List添加的其他设置方法。

尝试应用Composite模式(其他模式建议?),因为所有这些类都是相当多的节点(带有子节点),但是具有它们自己的对应属性。 提出一个共同的基类

class Node<X> extends ArrayList<X>

但不知道如何从子类中将@XmlElement(name =“ depends_on_subclass”)应用于基类。 除非在子类中,并且在虚拟getSelf()方法上(例如在Table中)添加@XmlElement,

class Table extends Node<Row>
{

    @XmlElement(name="row")
    public Table getSelf()
    {
        return this;
    }
:
:

还有其他更好的建议吗? 提前致谢。

我假设您为此使用xjc,在这种情况下,您可以使用jaxb插件

我使用了这个插件http://confluence.highsource.org/display/J2B/Inheritance+plugin

然后将类似这样的内容添加到您的xml中。 就是这样。 他们将扩展给定的基类。

<xsd:element name="attributesRequest">
    <xsd:annotation>
        <xsd:appinfo>
            <inheritance:extends>com.work.autofill.common.Filter</inheritance:extends>
        </xsd:appinfo>
    </xsd:annotation>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM