2011-02-24 48 views
4

JAXBContext是线程安全的,但Unmarshaller不是。我想使解组的请求范围豆,和我这样做:弹簧作用域代理和JAXB

<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的类属性指向那个?似乎有点奇怪。线索?

编辑:

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

回答

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>