Reputation: 26904
Spring's default scope is singleton
. However, for MVC controllers, which run to serve web requests and, in my project, may contain @Autowired
prototype beans, I would like to force them by default to be at least request
-scoped, if not prototype, without annotating each and every controller with @Scope
Do you think it is possible? If so, how?
Upvotes: 0
Views: 1895
Reputation: 5004
you should add @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
on your beans ( not controllers ) which are going to be autowired into your controller. This way, you inform spring to create proxy around your autowired bean - so its possible to use that in singleton class.
Upvotes: 2
Reputation: 2862
A composed annotation might be a good fit.
@Target(TYPE)
@Retention(RUNTIME)
@Documented
@Controller
@Scope(WebApplicationContext.SCOPE_REQUEST)
public @interface RequestScopedController { ... }
The controllers would use @RequestScopedController
annotation instead of @Controller
.
Upvotes: 1