Bird
Raised Fist0
Spring Bootframework~10 mins

@Scope for bean scope in Spring Boot - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - @Scope for bean scope
Define Bean with @Scope
↓
Spring Container creates Bean
↓
Check Scope Type
↓
Singleton
↓
One instance
↓
Bean injected/used
Spring reads @Scope on a bean, decides how many instances to create, then injects the bean accordingly.
Execution Sample
Spring Boot
@Bean
@Scope("prototype")
public MyBean myBean() {
  return new MyBean();
}
Defines a bean with prototype scope so Spring creates a new instance each time it is requested.
Execution Table
StepActionScope CheckBean Instance CreatedInstance Count
1Spring starts and reads bean definitionprototypeNo0
2First request for beanprototypeYes1
3Second request for beanprototypeYes2
4Third request for beanprototypeYes3
5No more requests-No3
💡 No more requests for the bean, prototype scope creates new instance each time
Variable Tracker
VariableStartAfter 1After 2After 3Final
instanceCount01233
Key Moments - 2 Insights
Why does Spring create a new bean instance each time with prototype scope?
Because the execution_table shows at each request (steps 2,3,4) a new instance is created, matching prototype behavior.
What happens if the scope is singleton instead?
Spring creates only one instance at startup and reuses it, so instanceCount would stay 1 after first creation.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, how many bean instances exist after the second request?
A1
B2
C3
D0
💡 Hint
Check the 'Instance Count' column at step 3 in execution_table
At which step does Spring decide not to create a new bean instance?
AStep 5
BStep 2
CStep 1
DStep 4
💡 Hint
Look for 'Bean Instance Created' = 'No' in execution_table
If the scope was singleton, how would the instanceCount change after multiple requests?
AIncrease by 1 each request
BReset to 0 after each request
CStay at 1 after first request
DIncrease only on odd requests
💡 Hint
Refer to key_moments explanation about singleton scope behavior
Concept Snapshot
@Scope annotation sets bean lifecycle.
Singleton: one instance per container.
Prototype: new instance per request.
Spring creates or reuses bean based on scope.
Use @Scope("prototype") for multiple instances.
Default is singleton scope.
Full Transcript
The @Scope annotation in Spring Boot controls how many instances of a bean Spring creates and manages. When a bean is marked with @Scope("prototype"), Spring creates a new instance every time the bean is requested. This contrasts with the default singleton scope, where Spring creates one instance and reuses it. The execution table shows Spring starting with zero instances, then creating a new one on each request for prototype scope. The variable tracker counts how many instances exist after each request. Key moments clarify why prototype scope creates new instances and how singleton scope differs. The visual quiz tests understanding of instance counts and scope behavior. Remember, @Scope controls bean lifecycle and instance creation in Spring.

Practice

(1/5)
1. What does the @Scope("prototype") annotation do in a Spring Boot bean?
easy
A. Creates a new bean instance per HTTP request
B. Creates only one shared instance of the bean for the entire application
C. Creates a new bean instance per HTTP session
D. Creates a new instance of the bean every time it is requested

Solution

  1. Step 1: Understand the prototype scope meaning

    The prototype scope means Spring creates a new bean instance each time it is requested from the container.
  2. Step 2: Compare with other scopes

    Unlike singleton which shares one instance, prototype always returns a fresh object.
  3. Final Answer:

    Creates a new instance of the bean every time it is requested -> Option D
  4. Quick Check:

    @Scope("prototype") = new instance each request [OK]
Hint: Prototype means new bean each request, singleton means shared [OK]
Common Mistakes:
  • Confusing prototype with singleton scope
  • Thinking prototype beans are shared
  • Mixing prototype with web scopes like session or request
2. Which of the following is the correct way to declare a singleton scoped bean using @Scope annotation in Spring Boot?
easy
A. @Scope("session")
B. @Scope("prototype")
C. @Scope("singleton")
D. @Scope("request")

Solution

  1. Step 1: Recall default and explicit singleton scope

    Singleton is the default scope in Spring, but explicitly it is declared as @Scope("singleton").
  2. Step 2: Verify other scopes

    prototype creates new instances, session and request are web scopes, not singleton.
  3. Final Answer:

    @Scope("singleton") -> Option C
  4. Quick Check:

    Singleton scope = @Scope("singleton") [OK]
Hint: Singleton scope is default but use @Scope("singleton") explicitly [OK]
Common Mistakes:
  • Using @Scope("prototype") for singleton beans
  • Confusing web scopes with singleton
  • Omitting @Scope and expecting prototype behavior
3. Consider this Spring Boot bean declaration:
@Component
@Scope("request")
public class UserRequestBean { }

What happens when this bean is injected into two different HTTP requests?
medium
A. Each request gets a new bean instance
B. Both requests share the same bean instance
C. Only the first request gets a bean, others get null
D. Bean is created once per session, shared across requests

Solution

  1. Step 1: Understand request scope behavior

    The request scope means Spring creates a new bean instance for each HTTP request.
  2. Step 2: Apply to two different requests

    Since the requests are different, each gets its own separate bean instance.
  3. Final Answer:

    Each request gets a new bean instance -> Option A
  4. Quick Check:

    @Scope("request") = new bean per HTTP request [OK]
Hint: Request scope means new bean per HTTP request [OK]
Common Mistakes:
  • Assuming request scope shares bean across requests
  • Confusing request scope with session scope
  • Thinking request scope behaves like singleton
4. 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?
medium
A. No error, injection works normally
B. Runtime error due to incompatible scopes
C. Compilation error due to missing @Scope annotation
D. Bean is created once and shared across all sessions

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]
Hint: 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
5. 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
A. @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
B. @Scope("prototype")
C. @Scope("singleton")
D. @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)

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]
Hint: 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard