Scope | Description |
---|---|
singleton | (Default) Scopes a single bean definition to a single object instance for each Spring IoC container. |
prototype | Scopes a single bean definition to any number of object instances. |
request | Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext. |
session | Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext. |
application | Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext. |
websocket | Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext. |
websocket :
@Component
@Scope(scopeName = "websocket", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class MyBean {
@PostConstruct
public void init() {
// Invoked after dependencies injected
}
// ...
@PreDestroy
public void destroy() {
// Invoked when the WebSocket session ends
}
}
@Controller
public class MyController {
private final MyBean myBean;
@Autowired
public MyController(MyBean myBean) {
this.myBean = myBean;
}
@MessageMapping("/action")
public void handle() {
// this.myBean from the current WebSocket session
}
}
自定义scope
void registerScope(String scopeName, Scope scope);
此方法在ConfigurableBeanFactory接口上声明,该接口可通过 Spring附带的BeanFactory大多数具体ApplicationContext实现上的属性获得。
该registerScope(…)方法的第一个参数是与范围关联的唯一名称。Spring容器本身中的此类名称示例为singleton和 prototype。该registerScope(…)方法的第二个参数是Scope您希望注册和使用的自定义实现的实际实例。
假设您编写了自定义Scope实现,然后注册它,如下面的示例所示。
Scope threadScope = new SimpleThreadScope();
beanFactory.registerScope("thread", threadScope);
/**
* A simple thread-backed {@link Scope} implementation.
*
* <p><b>NOTE:</b> This thread scope is not registered by default in common contexts.
* Instead, you need to explicitly assign it to a scope key in your setup, either through
* {@link org.springframework.beans.factory.config.ConfigurableBeanFactory#registerScope}
* or through a {@link org.springframework.beans.factory.config.CustomScopeConfigurer} bean.
*
* <p>{@code SimpleThreadScope} <em>does not clean up any objects</em> associated with it.
* It is therefore typically preferable to use a request-bound scope implementation such
* as {@code org.springframework.web.context.request.RequestScope} in web environments,
* implementing the full lifecycle for scoped attributes (including reliable destruction).
*
* <p>For an implementation of a thread-based {@code Scope} with support for destruction
* callbacks, refer to
* <a href="https://www.springbyexample.org/examples/custom-thread-scope-module.html">Spring by Example</a>.
*
* <p>Thanks to Eugene Kuleshov for submitting the original prototype for a thread scope!
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @since 3.0
* @see org.springframework.web.context.request.RequestScope
*/
public class SimpleThreadScope implements Scope {
private static final Log logger = LogFactory.getLog(SimpleThreadScope.class);
private final ThreadLocal<Map<String, Object>> threadScope =
new NamedThreadLocal<Map<String, Object>>("SimpleThreadScope") {
@Override
protected Map<String, Object> initialValue() {
return new HashMap<>();
}
};
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
Map<String, Object> scope = this.threadScope.get();
Object scopedObject = scope.get(name);
if (scopedObject == null) {
scopedObject = objectFactory.getObject();
scope.put(name, scopedObject);
}
return scopedObject;
}
@Override
@Nullable
public Object remove(String name) {
Map<String, Object> scope = this.threadScope.get();
return scope.remove(name);
}
@Override
public void registerDestructionCallback(String name, Runnable callback) {
logger.warn("SimpleThreadScope does not support destruction callbacks. " +
"Consider using RequestScope in a web environment.");
}
@Override
@Nullable
public Object resolveContextualObject(String key) {
return null;
}
@Override
public String getConversationId() {
return Thread.currentThread().getName();
}
}