4

JAXBContext 是线程安全的,但 Unmarshaller 不是。我想让解组器成为请求范围 bean,我正在这样做:

<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>

但问题是我收到以下错误:

创建代理时无法确定目标类型

我已经阅读了带有 AOP 的 Spring session scope bean 中的问题,这表明我应该告诉 spring 更多关于我想要创建的类型,但是要创建的类型是一个接口。我是否应该寻找将基于 JAXB 实现实例化的实际类型,并使 unmarshaller bean 的类属性指向它?似乎有点奇怪。线索?

编辑:

好吧我的错。这实际上有效,只是在我的单元测试中失败了。春季测试中的请求范围 bean很有帮助。

4

1 回答 1

3

尝试使用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>
于 2011-02-24T18:41:38.630 回答