Spring / Spring Beans
Can a singleton bean hold a request- or session-scoped bean?
Not directly, it would cause a scope mismatch. You'll get an error unless you use a proxy mode.
@Component @Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS) public class RequestBean {} @Component @Scope("singleton") public class SingletonBean { @Autowired private RequestBean requestBean; }
The RequestBean is injected as a proxy that resolves the correct instance for each request.
More Related questions...