What does the @Scope annotation do in Spring Boot?
The @Scope annotation defines the lifecycle and visibility of a Spring bean, controlling how many instances of the bean are created and how long they live.
Click to reveal answer
beginner
Name the default scope of a Spring bean when @Scope is not specified.
The default scope is 'singleton', meaning only one instance of the bean is created and shared across the application context.
Click to reveal answer
intermediate
What is the difference between 'singleton' and 'prototype' scopes in Spring?
'Singleton' creates one shared instance per Spring container, while 'prototype' creates a new instance every time the bean is requested.
Click to reveal answer
beginner
How do you specify a prototype scope for a bean using @Scope?
Use @Scope("prototype") above the bean class or method to tell Spring to create a new instance each time the bean is requested.
Click to reveal answer
intermediate
What scopes are available in Spring Boot besides singleton and prototype?
Other scopes include 'request' (one bean per HTTP request), 'session' (one bean per HTTP session), and 'application' (one bean per ServletContext).
Click to reveal answer
What is the default scope of a Spring bean if @Scope is not used?
Aprototype
Bsingleton
Crequest
Dsession
✗ Incorrect
By default, Spring beans are singleton scoped, meaning one shared instance per container.
Which @Scope value creates a new bean instance every time it is requested?
Aprototype
Bapplication
Csingleton
Dsession
✗ Incorrect
The 'prototype' scope creates a new instance each time the bean is requested.
Which scope is best for beans that should live only during a single HTTP request?
Arequest
Bsingleton
Cprototype
Dsession
✗ Incorrect
The 'request' scope creates one bean instance per HTTP request.
How do you declare a bean with session scope in Spring Boot?
A@Scope("singleton")
B@Scope("prototype")
C@Scope("session")
D@Scope("application")
✗ Incorrect
Use @Scope("session") to create one bean instance per HTTP session.
If you want a bean to be shared across the entire web application, which scope should you use?
Arequest
Bsession
Cprototype
Dapplication
✗ Incorrect
The 'application' scope shares one bean instance across the whole ServletContext.
Explain how the @Scope annotation affects bean lifecycle and give examples of common scopes.
Think about how many instances are created and how long they live.
You got /3 concepts.
Describe a scenario where using prototype scope is better than singleton scope.
Consider when you want fresh data or state per use.
You got /3 concepts.
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
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.
Step 2: Compare with other scopes
Unlike singleton which shares one instance, prototype always returns a fresh object.
Final Answer:
Creates a new instance of the bean every time it is requested -> Option D
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
Step 1: Recall default and explicit singleton scope
Singleton is the default scope in Spring, but explicitly it is declared as @Scope("singleton").
Step 2: Verify other scopes
prototype creates new instances, session and request are web scopes, not singleton.
Final Answer:
@Scope("singleton") -> Option C
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
Step 1: Understand request scope behavior
The request scope means Spring creates a new bean instance for each HTTP request.
Step 2: Apply to two different requests
Since the requests are different, each gets its own separate bean instance.
Final Answer:
Each request gets a new bean instance -> Option A
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
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.
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.
Final Answer:
Runtime error due to incompatible scopes -> Option B
Quick Check:
Session bean into singleton without proxy = runtime error [OK]
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
Step 1: Identify session scope for per-session bean
The session scope creates one bean instance per HTTP session, which fits the requirement.
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.
Final Answer:
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.