我在我的評論中提到了另一種應用程序。
這是我們在我們的RCP應用程序中使用的。除了我們通過網絡進行編組/解組之外,我們使用JAXWS而不僅僅是JAXB。
我用這種堆棧的有些經驗,所以這裏有一個腳踏啓動器爲您提供:
/**
* Your UI POJO-s should extend this class.
*/
public abstract class UIModel<T extends UIModel> {
protected final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
/**
* This comes handy at times
*/
public void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
//....
}
/**
* And this too, trust me.
*/
public void deepCopy(final T of) {
removePropertyChangeListener(propertyChangeListener);
//It's from Spring Framework but you can write your own. Spring is a fat-ass payload for a Java-SE application.
BeanUtils.copyProperties(of, this, IGNORED_ON_CLIENT);
addPropertyChangeListener(propertyChangeListener);
}
}
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}
}
/**
* Example of a UI POJO.
*/
public class Car extends UIModel<Car> {
private String make;
private int numberOfWheels;
//... etc.
/**
* Example of a setter
*/
public void setMake(String make) {
propertyChangeSupport.firePropertyChange("make", this.make, this.make = make);
}
public String getMake() {
return make;
}
//... etc.
}
我不知道多久你的模式定義的變化,但有支持此模式;
/**
* New application (compiled with the class below) can open a file saved by the old application.
*/
public class Car2 extends Car {
private String fuelType; // Example of a new field
public void setFuelType(String fuelType) {
propertyChangeSupport.firePropertyChange("fuelType", this.fuelType, this.fuelType = fuelType);
}
//... etc.
}
這樣老應用程序可以打開新的XML輸出。從此類的源代碼中刪除字段將導致RuntimeException,因爲JAXB仍在尋找它。 如果你的客戶總是最新的,那麼你根本就不應該在意這一點。
當處理Java集合並過分地繼承時,您將遇到JAXB問題,您可以使用Googling @XmlRootElement和@XmlSeeAlso註釋來解決這些問題。
您可以嘗試反過來使用XJC:用必要的註釋聲明您的Java類並從它們生成XML模式。這樣你可以實現UI綁定所需的setter。假設你將它綁定到一個UI,你將只使用一組屬性/元素,並且當模式的顯示部分改變時你需要編寫代碼。新的XML元素不會打擾JAXB解組;他們將被忽略。 –