[英]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.