You can use a custom scope called SimpleThreadScope .
From the Spring documentation:
As with Spring 3.0 , the stream area is accessible but not registered by default. See the documentation for SimpleThreadScope for more information. For instructions on registering this or any other custom volume, see section 3.5.5.2, βUsing Custom Volumeβ .
Here is an example of how to register a SimpleThreadScope region:
Scope threadScope = new SimpleThreadScope(); beanFactory.registerScope("thread", threadScope);
Then you can use it in the definition of bean:
<bean id="foo" class="foo.Bar" scope="thread">
You can also declare the registration area:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <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 id="foo" class="foo.Bar" scope="thread"> <property name="name" value="bar"/> </bean> </beans>
Jean-philippe bond
source share