我通过Spring的http调用者公开了一个非线程安全的bean。我想要的是每个远程调用都应该获得bean的新实例。我首先将范围设置为我在Dispatcher servlet XML中公开的bean的原型。但它似乎仍然只创建一个实例。所以所有客户端线程都同时访问同一个bean实例。
接下来,我还在客户端spring-config.xml中将scopr设置为HttpInvokerProxyFactoryBean的原型。但即使这样,我也会看到返回的bean的一个实例。
关于我做错的任何想法?或者有其他人遇到过这个问题。
提前致谢。
以下是相关摘录
的DispatcherServlet-servlet.xml中
<bean id="fuBeanImpl" class="com.fubar.FuBeanImpl" scope="prototype">
</bean>
<bean id="fuBeanService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="fuBeanImpl"/>
<property name="serviceInterface" value="com.fubar.FuBean"/>
</bean>
弹簧-config.xml中
<bean id="fuBeanService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean" scope="prototype">
<property name="serviceUrl">
<value>http://fubar/fuBeanService</value>
</property>
<property name="serviceInterface">
<value>com.fubar.FuBean</value>
</property>
<property name="httpInvokerRequestExecutor">
<bean class="org.springframework.remoting.httpinvoker.CommonsHttpInvokerRequestExecutor"/>
</property>
</bean>
答案 0 :(得分:1)
这是因为你的HttpInvokerServiceExporter
bean仍然是一个单例,并且它引用了原型范围的fuBeanImpl
bean。因此,导出器获取FuBeanImpl
的单个实例,并且从不要求新的实例。这是单引脚范围的bean引用on-singleton-scoped bean的问题 - 引用“折叠”原型,有效。
您还需要使HttpInvokerServiceExporter
成为原型范围的bean,尽管这可能会产生副作用。例如,你没有告诉我们什么是HttpInvokerServiceExporter
- 可能是某个地址的url-mapping定义?
编辑:由于您已澄清您使用的是SimpleUrlhandlerMapping
,因此您可以执行的操作是使用名称注入处理程序bean,而不是对它的直接bean引用。这意味着处理程序bean(即fuBeanService
bean)可以是原型,即使SimpleUrlhandlerMapping
是单例。
答案 1 :(得分:1)
关于几乎完全相同的问题还有另一个当前问题Prototype Scope not working。
这两种解决方案也适用于此: