开发者

How to use Spring SimpleThreadScope?

Spring 3.0 defines a SimpleThreadScope. Looking at all known implementing classes of the Scope interface, I see: AbstractRequestAttributesScope, PortletContextScope, RequestScope, ServletContextScope, SessionScope, SimpleThreadScope.

The first observation is that I don't see a PrototypeScope, and don't understand why.

My question, though, is how to use SimpleThreadScope, since RequestScope becomes "request" in XML or annotation, so I tried to use "simplethread" and "simpleThread", but they don't work.

I get the following message:

Caused by: java.lang.IllegalStateException: No Scope registered for scope 'simpleThread'
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)

Does it mean that SimpleThreadScope should be used only programmatically, not through XML or an开发者_运维知识库notations? And how to use it?


SimpleThreadScope is not registered by default, so that you need to register it manually in order to use it, see 3.5.5.2 Using a custom scope.

singleton and prototype scopes don't have their Scope classes since they are hardcoded into bean factory.


Here is one way to use SimpleThreadScope in Spring Boot.

Go to the Application class where your project starts when executed.

Then will need to import the following:

import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.config.CustomScopeConfigurer;
import org.springframework.context.support.SimpleThreadScope;

Create a static method that returns an object of type CustomScopeConfigurer and apply the @Bean annotation to it:

@Bean
public static CustomScopeConfigurer yourCustomScopeConfigurer() {}

This will indicate that this method will produce a bean to be managed by the Spring container.

Then, create a new CustomScopeConfigurer which you will setup later.

@Bean
public static CustomScopeConfigurer yourCustomScopeConfigurer() {
  final CustomScopeConfigurer newConfigurer = new CustomScopeConfigurer();
  return newConfigurer;
}

We use the keyword final here to create a constant object which you can setup but you can't modify. In other words, the reference variable cannot be re-bound to another object. The internal state of the object pointed by this reference variable can be changed.

Next, we need to register all of the supplied scopes, here is where we use the SimpleThreadScope via a HashMap:

@Bean
public static CustomScopeConfigurer yourCustomScopeConfigurer() {
  final CustomScopeConfigurer newConfigurer = new CustomScopeConfigurer();
  Map<String, Object> newScopes = new HashMap<String, Object>();
  newScopes.put("thread", new SimpleThreadScope());
  newConfigurer.setScopes(newScopes);
  return newConfigurer;
}

Optional

You might want to use the @EnableAsync annotation which enables Spring's asynchronous method execution capability:

import org.springframework.scheduling.annotation.EnableAsync;
...
@EnableAsync
public class YourProjectApplication{
...
}

Note: the three dots means code between those lines.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜