Spring / Spring Beans
Explain the Scope Injection problem and provide a solution to fix it.
Injecting longer-lived into shorter-lived: This works as expected. For example, injecting a singleton bean (which is created once for the entire application) into a prototype bean (new instance for each request) means all prototype instances share the single singleton instance. This is common for stateless dependencies.
Injecting shorter-lived into longer-lived (the "scoped bean injection problem"): If you directly inject a prototype bean into a singleton bean using @Autowired, only one instance of the prototype bean is created when the singleton bean is initialized and that same instance is reused throughout the application's life. This defeats the purpose of the prototype scope.
Solutions for the "Scoped Bean Injection Problem":
To ensure a new instance of a shorter-lived bean is provided on each access within a longer-lived bean, use the following mechanisms:
- @Lookup method injection: This is the cleanest way for prototype beans. Spring overrides the annotated method at runtime to return a new instance of the requested bean whenever the method is called.
@Component @Scope("singleton") public abstract class SingletonBean { @Lookup public abstract PrototypeBean getPrototypeBean(); // ... }
-
ObjectFactory or Provider injection: Injecting ObjectFactory
or Provider (from javax.inject) allows you to explicitly call a getObject() or get() method to request a new instance whenever needed. @Component @Scope("singleton") public class SingletonBean { @Autowired private ObjectFactory<PrototypeBean> prototypeBeanFactory; public void doSomething() { PrototypeBean prototypeBean = prototypeBeanFactory.getObject(); // use the new instance } }
- Scoped Proxies (@Scope(proxyMode = ...)): For web-specific scopes (request, session, application, websocket), or for a more generic solution with prototype, you can use scoped proxies. Spring injects a proxy that looks like the target bean but in reality delegates to the actual bean instance for the correct, active scope (e.g., a new instance for each request or session).
@Component @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS) public class RequestScopedBean { // ... } @Component @Scope("singleton") public class SingletonBean { @Autowired private RequestScopedBean requestScopedBean; // ... }
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
