编程语言
首页 > 编程语言> > java – spring scoped proxy和JAXB

java – spring scoped proxy和JAXB

作者:互联网

JAXBContext是线程安全的,但Unmarshaller不是.我想让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>

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

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

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

编辑:

好吧,我的错误.这实际上有效,它在单元测试中失败了. request scoped beans in spring testing很有帮助.

解决方法:

尝试使用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>

标签:java,spring,jaxb,dynamic-proxy
来源: https://codeday.me/bug/20190630/1340014.html