Bird
0
0

Given this bean declaration:

medium📝 Debug Q14 of 15
Spring Boot - Spring Annotations
Given this bean declaration:
@Component
@Scope("session")
public class ShoppingCart { }

What is the likely error if you try to inject this bean into a singleton scoped bean without special handling?
ANo error, injection works normally
BRuntime error due to incompatible scopes
CCompilation error due to missing @Scope annotation
DBean is created once and shared across all sessions
Step-by-Step Solution
Solution:
  1. Step 1: Understand scope mismatch problem

    Injecting a session-scoped bean into a singleton bean causes a scope mismatch because singleton lives longer than session beans.
  2. Step 2: Result of mismatch without proxy

    Without proxy or special configuration, Spring throws a runtime error as it cannot inject a shorter-lived bean into a longer-lived one safely.
  3. Final Answer:

    Runtime error due to incompatible scopes -> Option B
  4. Quick Check:

    Session bean into singleton without proxy = runtime error [OK]
Quick Trick: Injecting shorter scope bean into singleton causes runtime error [OK]
Common Mistakes:
  • Assuming injection always works regardless of scope
  • Ignoring need for proxyMode in such injections
  • Confusing compilation and runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes