0
0
Spring Bootframework~10 mins

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

Choose your learning style9 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.