spring scoped proxy and JAXB

1.5k Views Asked by At

JAXBContext is thread-safe but the Unmarshaller is not. I'd like to make the unmarshaller a request scope bean, and I'm doing this:

<bean id="jaxbContext" class="javax.xml.bind.JAXBContext"
    factory-method="newInstance">
    <constructor-arg>
        <list>
            <value type="java.lang.Class">MyType</value>
        </list>
    </constructor-arg>
</bean>
<bean id="unmarshaller" class="javax.xml.bind.Unmarshaller"
    factory-bean="jaxbContext" factory-method="createUnmarshaller"
    scope="request">
    <aop:scoped-proxy />
</bean>

But the problem is I'm getting the following error:

Target type could not be determined at the time of proxy creation

I've read problem in Spring session scope bean with AOP which suggests that I should tell spring more about the type I'd like created, but the type to create is an interface. Am I supposed to hunt down the actual type that will get instantiated based on the JAXB implementation, and make the unmarshaller bean's class attribute point to that? Seems kind of weird. Clues?

EDIT:

Ok my mistake. This actually works, it just fails in my unit test. request scoped beans in spring testing was helpful.

1

There are 1 best solutions below

0
On

Try using lazy-init="true":-

<bean id="unmarshaller" 
    class="javax.xml.bind.Unmarshaller"
    factory-bean="jaxbContext" 
    factory-method="createUnmarshaller"
    scope="request" 
    lazy-init="true">

    <aop:scoped-proxy />

</bean>