Bird
0
0

You want a Spring Boot bean to be created once per HTTP session and also want to inject it into a singleton bean. What is the correct way to handle this scenario?

hard📝 Conceptual Q8 of 15
Spring Boot - Spring Annotations
You want a Spring Boot bean to be created once per HTTP session and also want to inject it into a singleton bean. What is the correct way to handle this scenario?
AUse @Scope("prototype") and inject directly into singleton
BDeclare the bean as singleton and manually manage session data
CDeclare the bean with @Scope("session") and use proxyMode = ScopedProxyMode.TARGET_CLASS
DUse @Scope("request") and inject into singleton bean
Step-by-Step Solution
Solution:
  1. Step 1: Understand session scoped bean injection into singleton

    Session scoped beans need proxies to be injected into singleton beans.
  2. Step 2: Use proxyMode to enable correct injection

    Setting proxyMode = ScopedProxyMode.TARGET_CLASS creates a proxy to handle session scope properly.
  3. Final Answer:

    Declare the bean with @Scope("session") and use proxyMode = ScopedProxyMode.TARGET_CLASS -> Option C
  4. Quick Check:

    Session scope + proxyMode needed for singleton injection [OK]
Quick Trick: Use scoped proxy to inject session bean into singleton [OK]
Common Mistakes:
  • Injecting session bean directly without proxy
  • Using prototype scope incorrectly
  • Confusing request with session scope

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes