JAXB specification states that JAXB context is thread safe. Because its creation is time consuming, it is encouraged to create/initialize it only once. How to do it properly in multi-module maven project? Passing classes as arguments will probably introduce some cyclic dependencies, passing strings as package names seems to be tedious and error-prone. Is there any recommended way of doing that?
Instead of having JAXB mapped classes in every module group them in a schema or jaxb or api module (or whatever name suits most). In that module you could put the factory to get the JAXBContext instance.
If you don't want to mix mapped classes in the same module, for every module that uses JAXB, extract mapped classes to another module. For example, if you have moduleA
, moduleB
, moduleC
, extract JAXB mapped classes to moduleA-schema
, moduleB-schema
, moduleC-schema
. This way you could put the factory for the JAXBContext instance in a utility module that depends on moduleA-schema
, moduleB-schema
, moduleC-schema
, but not on the original modules, so you could avoid cyclic dependencies.