Home > java, jaxb, spring > Spring: Injecting JAXB (Un)Marshaller

Spring: Injecting JAXB (Un)Marshaller

Create a JAXB context passing an array of root element classes:

	<bean id="jaxbContext" class="javax.xml.bind.JAXBContext"
		factory-method="newInstance">
		<constructor-arg>
			<list>
				<value type="java.lang.Class">my.example.rootclass.Root</value>
			</list>
		</constructor-arg>
	</bean>

Create prototypes for the marshallers/unmarshallers

	<!-- Pool (un)marshallers to improve performance -->
	<bean id="marshallerTarget" class="javax.xml.bind.Marshaller"
		factory-bean="jaxbContext" factory-method="createMarshaller"
		scope="prototype">
	</bean>

	<bean id="unmarshallerTarget" class="javax.xml.bind.Unmarshaller"
		factory-bean="jaxbContext" factory-method="createUnmarshaller"
		scope="prototype">
	</bean>

Wrap these in pool targets:

	<bean id="poolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource">
		<property name="targetBeanName" value="marshallerTarget" />
		<property name="maxSize" value="25" />
	</bean>

	<bean id="unmarshallerPoolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource">
		<property name="targetBeanName" value="unmarshallerTarget" />
		<property name="maxSize" value="25" />
	</bean>

And create pools for reusing marshallers/unmarshallers:

	<bean id="marshaller" class="org.springframework.aop.framework.ProxyFactoryBean">
		<qualifier value="marshaller" />
		<property name="targetSource" ref="poolTargetSource" />
	</bean>

	<bean id="unmarshaller" class="org.springframework.aop.framework.ProxyFactoryBean">
		<qualifier value="unmarshaller" />
		<property name="targetSource" ref="unmarshallerPoolTargetSource" />
	</bean>

This will create one context that will be used to create up to 25 marshallers/unmarshallers. Each will be created on demand until there are 25 in use simultaneously, then calls to them will block until one becomes available. Be careful changing the state on the (un)marshallers. It would probably be a better idea to create prototypes for each configuration and inject a separate pool for each.

These can be injected in the application context or using annotations:

    @Autowired
    @Qualifier("unmarshaller")
    private Unmarshaller unmarshaller;

    @Autowired
    @Qualifier("marshaller")
    private Marshaller marshaller;

Advertisement
Categories: java, jaxb, spring
  1. michal
    April 30, 2010 at 7:41 pm | #1

    thank you – you helped me a lot :)

    best regards,
    michal

  2. Jan
    December 12, 2010 at 1:37 pm | #2

    Thanks for the tip,

    but how do you create a jaxbcontext which can handle multiple packages?

    • Andrew Hahn
      December 22, 2010 at 6:45 am | #3

      Multiple packages? Do you mean multiple root element classes? Simply add another root element class to the constructor-arg list.

  3. rumberomelo
    February 9, 2011 at 9:06 am | #4

    It’s the best jaxb usaged I have seen

    thanks

  4. David
    February 25, 2011 at 9:53 pm | #5

    Very nice sir!

  5. gps470
    November 23, 2011 at 6:25 pm | #6

    To the point….. exactly what I was looking for.

  1. July 13, 2011 at 3:00 pm | #1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.