anstarovoyt
anstarovoyt

Reputation: 8136

Customize object creation over JAXB deserialization

I have the class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ClassFqn", propOrder = { <some properties> })
public class ClassFqn
{       
   //... Here should be fields, constructor and logic
}

Objects are immutable and I use cached objects pool factory instead of direct creation.

Can I use this pool factory when I do JAXB deserialization for these objects?

Upvotes: 2

Views: 1459

Answers (1)

bdoughan
bdoughan

Reputation: 149027

There are a couple of ways that you could support this use case.

Option #1 - Use @XmlType to specify Factory Class & Method

You can use the @XmlType annotation to specify a factory class and method:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
    name = "ClassFqn", 
    propOrder = { <some properties> }
    factoryClass=ClassFqnFactory.class,
    factoryMethod="createClassFqn")
public class ClassFqn
{       
   //... Here should be fields, constructor and logic
}

For More Information

Option #2 - Use an XmlAdapter

An XmlAdapter can also be leveraged. Essentially you read the data into a temporary object and then implement the XmlAdapter to use your own mechanisms to create the final object.

For More Information

Upvotes: 3

Related Questions