Bird
0
0

You want a Spring Boot bean to create a new instance for each HTTP session but share the same instance within that session. Which @Scope annotation and configuration should you use?

hard📝 Application Q15 of 15
Spring Boot - Spring Annotations
You want a Spring Boot bean to create a new instance for each HTTP session but share the same instance within that session. Which @Scope annotation and configuration should you use?
A@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
B@Scope("prototype")
C@Scope("singleton")
D@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
Step-by-Step Solution
Solution:
  1. Step 1: Identify session scope for per-session bean

    The session scope creates one bean instance per HTTP session, which fits the requirement.
  2. Step 2: Use proxyMode for injection into singleton beans

    Adding proxyMode = ScopedProxyMode.TARGET_CLASS allows safe injection into other beans by creating a proxy that resolves the correct session instance.
  3. Final Answer:

    @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) -> Option A
  4. Quick Check:

    Session scope + proxyMode = new instance per session shared inside session [OK]
Quick Trick: Use session scope with proxyMode for per-session shared bean [OK]
Common Mistakes:
  • Using prototype scope which creates new instance every injection
  • Using singleton scope which shares one instance globally
  • Using request scope which creates new instance per HTTP request

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes