0

I have a bean with @RequestScope in it, and when I inject it in one of my Singletons, it is injected as a singleton and not as a request scope. However, if I change the @RequestScope to @Scope( value = "request", scopeName = "request", proxyMode = ScopedProxyMode.TARGET_CLASS), Spring creates the bean as request scope and injects them to the singleton correctly.

I read the documentation of Spring regarding this:

The JSR-330 default scope is like Spring’s prototype. However, in order to keep it consistent with Spring’s general defaults, a JSR-330 bean declared in the Spring container is a singleton by default. In order to use a scope other than singleton, you should use Spring’s @Scope annotation. javax.inject also provides a @Scope annotation. Nevertheless, this one is only intended to be used for creating your own annotations.

Does this mean also that @RequestScoped is really being ignored by Spring? Is there any Provider/Resolver that resolves this issue with Spring? As much as possible I want to use the @RequestScoped annotation instead of @Scope annotation of spring as we are required to use JSR annotations only

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

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
qaxi
  • 472
  • 1
  • 4
  • 13
  • Write your own if you really want, there are samples for this around the internet. The documentation is pretty clear on that it doesn't work . – M. Deinum Jul 18 '16 at 06:50
  • I saw some blogs about it but they were written years ago so I was wondering if Spring already addressed the issue since JSR 330 is a standard – qaxi Jul 18 '16 at 07:01
  • 1
    As mentioned no it hasn't and won't as explained by the recent documentation which you yourself are pointing at. – M. Deinum Jul 18 '16 at 07:04

1 Answers1

1

As mentioned by by M. Deinum, Spring doesn't support @RequestScoped out of the box. I had to create a ScopeMetadataResolver to convert @RequestScoped into Spring @Scope

I referred here for my custom resolver:

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

qaxi
  • 472
  • 1
  • 4
  • 13