0% found this document useful (0 votes)
10 views

6. Bean Scopes

Uploaded by

govardhan v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

6. Bean Scopes

Uploaded by

govardhan v
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Bean Scopes

1. What is the Singleton scope in Spring?

Singleton scope means Spring creates only one instance of the


bean per container. This single instance is shared whenever the
bean is needed.
Bean Scopes

2. Is Singleton the default scope for Spring beans?

Yes, if you don't specify a scope, Spring uses Singleton by


default.
Bean Scopes

3. How do you define a bean as Singleton in Spring?

You can use the @Scope annotation or XML configuration:


Or in XML:
Bean Scopes

4. Are Singleton beans in Spring thread-safe?

No, Spring Singleton beans are not automatically thread-safe.


You need to handle thread safety yourself if the bean will be used
by multiple threads.
Bean Scopes

5. What is the Prototype scope in Spring?

Prototype scope means a new instance of the bean is created


every time it's requested from the container.
Bean Scopes

6. When should you use Prototype scope?

Use Prototype scope when you want a new instance each time,
like for stateful beans where each instance should have its own
data.
Bean Scopes

7. How do you define a Prototype bean in Spring?

You can use the @Scope annotation or XML configuration:


Or in XML:
Bean Scopes

8. What's the main difference between Singleton and Prototype


scopes?

Singleton creates one shared instance, while Prototype creates a


new instance each time the bean is requested.
Bean Scopes

9. What are web-aware scopes in Spring?

Web-aware scopes are scopes that are only available in web


applications. They include request, session, and application
scopes.
Bean Scopes

10. What is the request scope?

In request scope, Spring creates a new instance of the bean for


each HTTP request. The bean lasts only for that request.
Bean Scopes

11. How does session scope work?

In session scope, Spring creates one instance of the bean per


user session. This instance lasts for the entire session.
Bean Scopes

12. What is the application scope?

Application scope is similar to Singleton, but for web


applications. One instance is created for the entire web
application and shared across all sessions.
Bean Scopes

13. How do you define a bean with request scope?

You can use the @Scope annotation:


Bean Scopes

14. Why do we need to specify a proxy mode for web-aware


scopes?

Proxy mode is needed because these scopes don't exist when


the application starts. The proxy stands in for the actual bean
until the proper scope (like a request) is available.
Bean Scopes

15. Can you create your own bean scope in Spring?

Yes, Spring allows you to create custom scopes by implementing


the Scope interface.
Bean Scopes

16. How do you implement a custom scope?

You need to create a class that implements the


org.springframework.beans.factory.config.Scope interface, then
register it with the container.
Bean Scopes

17. What methods do you need to implement for a custom


scope?

You need to implement get(), remove(),


registerDestructionCallback(), and getConversationId() methods.
Bean Scopes

18. How do you register a custom scope with Spring?

You can register a custom scope using the


ConfigurableBeanFactory's registerScope method:
Bean Scopes

19. When might you need to create a custom scope?

You might create a custom scope for special business needs, like
a scope that lasts for a user's shopping cart session or a scope
tied to a specific workflow.

You might also like