[英]Spring's threadlocal bean is behaving like a singleton
我們使用的是Spring 4.0.6.RELEASE,Java 8,而Tomcat是我們的應用托管引擎。
我們有一個春豆,看起來像這樣:
@Service
@Scope("thread")
public class Foo {
private Bar bar;
public void setBar(Bar bar){
this.bar = bar;
}
}
問題在於,當這個bean被注入到不同的線程中時,所有線程都會得到同一個bean。 每個線程都不會像我期望的那樣得到它自己的bean。 Bean被注入@Autowired
。 要獲取線程本地bean,還需要做其他事情嗎?
我像這樣在xml中注冊了作用域:
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="thread">
<bean class="org.springframework.context.support.SimpleThreadScope"/>
</entry>
</map>
</property>
</bean>
這里有一個陷阱,您必須額外提及在bean之上創建哪種代理-該代理理解作用域並將其基礎的bean管理到相關作用域。 這應該為您工作:
@Service
@Scope(value="thread", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class Foo {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.