user1899353
user1899353

Reputation: 23

Is there a way to make a bean thread-scope?

I want an object to be bound to the current thread in one Spring bean that is singleton scope. Is there a way to do this?

Upvotes: 2

Views: 727

Answers (1)

ElderMael
ElderMael

Reputation: 7111

Maybe implementing an aspect with Spring's ThreadLocalTargetSource. It is explained very well in the documentation:

ThreadLocal target sources are useful if you need an object to be created for each incoming request (per thread that is). The concept of a ThreadLocal provide a JDK-wide facility to transparently store resource alongside a thread. Setting up a ThreadLocalTargetSource is pretty much the same as was explained for the other types of target source:

<bean id="threadlocalTargetSource" class="org.springframework.aop.target.ThreadLocalTargetSource">
  <property name="targetBeanName" value="businessObjectTarget"/>
</bean>

Upvotes: 4

Related Questions