0

我有一个 bean @RequestScope,当我将它注入到我的一个单例中时,它是作为单例注入的,而不是作为请求范围注入的。但是,如果我将 更改@RequestScope@Scope( value = "request", scopeName = "request", proxyMode = ScopedProxyMode.TARGET_CLASS),Spring 会将 bean 创建为请求范围并将它们正确地注入到单例中。

我阅读了有关此的 Spring 文档:

JSR-330 默认作用域类似于 Spring 的原型。但是,为了与 Spring 的一般默认值保持一致,在 Spring 容器中声明的 JSR-330 bean 默认为单例。为了使用单例以外的范围,您应该使用 Spring 的 @Scope 注释。javax.inject 还提供了一个@Scope 注解。然而,这个仅用于创建您自己的注释。

这是否也意味着@RequestScopedSpring 真的忽略了这一点?是否有任何 Provider/Resolver 可以使用 Spring 解决此问题?我想尽可能地使用@RequestScoped注解而不是@Scopespring的注解,因为我们只需要使用JSR注解

http://docs.spring.io/spring/docs/4.2.5.RELEASE/spring-framework-reference/html/beans.html#beans-standard-annotations-limitations

4

1 回答 1

1

正如 M. Deinum 所提到的,Spring 不支持@RequestScoped开箱即用。我必须创建一个ScopeMetadataResolver转换@RequestScoped成 Spring@Scope

我在这里提到了我的自定义解析器:

https://github.com/matzew/spring-cdi-bridge/blob/master/src/main/java/net/wessendorf/spring/scopes/CdiScopeMetadataResolver.java

于 2016-07-18T09:10:21.767 回答