Skip to main content

Should JAXB2 provide POJO lifecycle methods?

3 replies [Last post]
wangzaixiang
Offline
Joined: 2004-11-23
Points: 0

JAXB2's POJO have no lifecycle method now, eg:

void beforeMarshal();
void afterUnmarshal();

or something like java.io.Seriablizable which support the POJO's serialize customization:

private void writeObject(ObjectOutputStream out)
private void readObject(ObjectInputStream in)

That will provide the JAXB2 more flexible.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kohsuke
Offline
Joined: 2003-06-09
Points: 0

Check out com.sun.xml.bind.ObjectLifecycle. It gives you exactly the beforeMarshal and the afterUnmarshal method. Granted, it's not a part of the spec yet, but if enough people find it useful, it would be easier for us to push that into the spec.

So please send us feedback.

Also, check out @XmlJavaTypeAdapter and XmlAdapter for achieving functionalities similar to readObject/writeObject.

I plan to write a blog about explaining how it works. Stay tuned...

wangzaixiang
Offline
Joined: 2004-11-23
Points: 0

Hi, kohsuke:

I think ObjectLifecycle is useful and cant replaced with the @XmlJavaTypeAdapter/XmlAdaptor.

maybe we can learning from Spring with declare the lifecycle method using annotation, without enforce it to implements the ObjectLifecycle interface, so the POJO is never depends on JAXB2 at all.

kohsuke
Offline
Joined: 2003-06-09
Points: 0

I agree that the ObjectLifecycle interface do not replace @XmlJavaTypeAdapter/XmlAdapter. I meant that they are both useful tools to control the on-the-wire representation and the in-memory representation.

We could certainly use annotations, and maybe from the evolution point of view it might be even desirable (you can add a new lifecycle method without breaking existing ones), but unlike Hibernate, those annotations will have to be a JAXB RI specific annotation, so the POJOs will still depend on JAXB2.