Why doesn't Spring provide a thread scope implementation? Has anyone used thread scoped beans with Spring in a web application context? There should be a standard, clear description of how to do this! (SpringByExample has a solution—I didn't test it—but it is not mainstream yet.)
相关问题
- java.lang.IllegalArgumentException: Cannot set to
- How to let a thread communicate with another activ
- Spring Data MongoDB - lazy access to some fields
- Declaring an explict object dependency in Spring
- Decoding body parameters with Spring
相关文章
- java JDK动态代理和cglib动态代理最后获取的代理对象都为null的问题
- org.xml.sax.SAXParseException; lineNumber: 7; colu
- SpringMVC如何把File封装到Map中?
- Spring: controller inheritance using @Controller a
- How to load @Configuration classes from separate J
- Java spring framework - how to set content type?
- Difference between Thread#run and Thread#wakeup?
- Java/Spring MVC: provide request context to child
In a web application context, you can use request scope that is roughly the same as using a thread scope. A request scoped bean is created for every request received by the server and discarded when the request is finished.
Consider that threads may be pooled on servers, and that's probably the reason why there is not a thread scope on Spring
Take a look at request scope: http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s04.html
Spring does provide a thread scope, but it is not registered by default.
The existing bean scopes are defined in the documentation, here.
The documentation then makes a note
Note that, similarly to the prototype scope, the thread scope
This thread scope implementation uses a
ThreadLocal
to store beans. You cannot detect aThread
ending/dying in Java, so the Spring IOC container cannot explicitly know when to remove the beans from theThreadLocal
and invoke any end of lifecycle methods. That responsibility then falls on the developer.Be careful where you use this scope. For example, in a thread pooling context, a bean might already have been created and stored in one of the pools' reused threads. Depending on your use case, that might be the incorrect behavior.
Actually it does provide a thread scope, since Spring 3.0. You might need to register it yourself instead of that it is used out-of-the-box.
See https://jira.springsource.org/browse/SPR-2581