0% found this document useful (0 votes)
70 views9 pages

27 Annotations Bean Scopes Overview

The document discusses Spring bean scopes, which determine the lifecycle and sharing of beans. The default scope is singleton, where a single bean instance is created and shared across all requests. Other scopes include prototype, where a new bean instance is created for each request, and request/session scopes which are scoped to the HTTP request or session respectively and only apply to web applications. Scopes can be explicitly specified using the @Scope annotation.

Uploaded by

Prakash Malviya
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)
70 views9 pages

27 Annotations Bean Scopes Overview

The document discusses Spring bean scopes, which determine the lifecycle and sharing of beans. The default scope is singleton, where a single bean instance is created and shared across all requests. Other scopes include prototype, where a new bean instance is created for each request, and request/session scopes which are scoped to the HTTP request or session respectively and only apply to web applications. Scopes can be explicitly specified using the @Scope annotation.

Uploaded by

Prakash Malviya
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/ 9

Bean Scopes with

Annotations
Bean Scopes
• Scope refers to the lifecycle of a bean


• How long does the bean live?


• How many instances are created?


• How is the bean shared?

www.luv2code.com
Default Scope

Default scope is singleton

www.luv2code.com
Refresher: What Is a Singleton?

• Spring Container creates only one instance of the bean, by default


• It is cached in memory


• All requests for the bean

• will return a SHARED reference to the SAME bean

www.luv2code.com
What is a Singleton?
Spring

Coach theCoach = context.getBean("tennisCoach", Coach.class);

...
TennisCoach

Coach alphaCoach = context.getBean("tennisCoach", Coach.class);

www.luv2code.com
Explicitly Specify Bean Scope

@Component
@Scope("singleton")
public class TennisCoach implements Coach {

...

www.luv2code.com
Additional Spring Bean Scopes

Scope Description

singleton Create a single shared instance of the bean. Default scope.

prototype Creates a new bean instance for each container request.

request Scoped to an HTTP web request. Only used for web apps.

session Scoped to an HTTP web session. Only used for web apps.

Scoped to a global HTTP web session. Only used for web


global-session
apps.

www.luv2code.com
Prototype Scope Example
Prototype scope: new object for each request

@Component
@Scope("prototype")
public class TennisCoach implements Coach {

...

www.luv2code.com
Prototype Scope Example

Prototype scope: new object for each request


Spring

TennisCoach
Coach theCoach = context.getBean("tennisCoach", Coach.class);

...

Coach alphaCoach = context.getBean("tennisCoach", Coach.class);


TennisCoach

www.luv2code.com

You might also like