Software Development

What are bean scopes in dependency injection in Spring Boot applications?

JO Asked by Jonathan Crane · 03-02-2025
0 upvotes 11,122 views 0 comments
The question

I need to understand how bean scopes affect dependency injection in Spring Boot. By default, everything seems to be a singleton, but how do prototype or request scopes behave when injected into a singleton bean during application development?

3 answers

0
BR
Answered on 20-04-2025

By default, dependency injection in Spring Boot uses the Singleton scope, meaning only one instance is created per application context. If you inject a Prototype bean (which creates a new instance every time it is requested) into a Singleton bean, it will only be injected once during startup. Consequently, the Singleton bean will keep using that same prototype instance forever. To fix this behavior and get a fresh instance every time, you must use a scoped proxy or lookup methods.

0
WA
Answered on 15-05-2025

Could you explain how a scoped proxy works under the hood to solve this issue? Does it negatively affect performance in heavy software development systems?

LA 18-05-2025

A scoped proxy inserts a smart proxy object instead of the real bean. When methods are called, the proxy fetches the correct short-lived bean from the context. The performance overhead is negligible for almost all enterprise web applications.

0
NI
Answered on 30-07-2025

For web apps, you also get request and session scopes, which tie the lifecycle of your injected beans directly to individual HTTP requests.

JO 02-08-2025

Nicole's point is crucial for user-specific data tracking. Using request scope ensures zero data leakage between different user threads accessing the server simultaneously.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session