Well, you probably can override JAXB's instantiation mechanism somehow to reuse an existing instance (your singleton) instead of creating a new instance.
As a side not, I thought this should work with ObjectFactory
but it did not for me:
Why is the ObjectFactory not used during unmarshalling?
But from my pont of view this would be not the best design. In this case any unmarshalling of the MyParams
will go into the same instance.
My suggestion would be to unmarshal into a distinct instance first and then merge these unmarshalled params into your singleton. Singleton per se is often an anti-pattern, but marrying it with JAXB unmarshalling would take the potential "anti-" even further.
Update
Seems like you can use @XmlType
annotation with factoryClass
and factoryMethod
to specify how the instances of MyParams
should be created.
So probably something like:
@XmlType(factoryClass=MyParamsFactory.class, factoryMethod="createMyParams")
public class MyParams {
...
public static MyParams getInstance() { ... }
}
public class MyParamsFactory {
public MyParams createMyParams() { return MyParams.getInstance(); }
}
should work.
However, as I said, I think it is not a good idea to unmarshal to singleton. This is an explicit warning.