Search code examples
javajaxbjava-ee-6

JAXBContext Managing


I have to generate a lot of xml snippets and store the into my database for later (re-)use. JaxB works fine, but i some questions came up:

  1. Should i have one JAXBContext for the whole application or for each response dto one? I would keep the context in a static variable in each root dto object. (initialized once)
  2. Is there no way to inject an marshaller in JEE6?

I want to keep the boilerplate code as small as possible.

greetings m

PS: XStream worked fine too, but i want to stick to the standards.


Solution

    • Yes, you need only one JAXBContext
    • You can always inject marshaller. (Best way is to use Spring)

       <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
           <property name="classesToBeBound">
              <list>
                  <value>com.package.YourClass</value>
               </list>
          </property>
      </bean>
      

    I don't properly understand what you mean be "sticking to standards", but you could give a look at JIBX. It generally provides better performance than JaxB