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>
但问题是我收到以下错误:
Target type could not be determined at the time of proxy creation
我读过 problem in Spring session scope bean with AOP这表明我应该更多地告诉 spring 我想创建的类型,但要创建的类型是一个接口(interface)。我是否应该寻找将基于 JAXB 实现实例化的实际类型,并使 unmarshaller bean 的类属性指向它?似乎有点奇怪。线索?
编辑:
好吧我错了。这确实有效,只是在我的单元测试中失败了。 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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5108950/