Bean Scopes
1) Singleton
- The default scope of every bean is singleton.
- One instance per context definition
- Be careful with static data
2) Prototype
- New instance every time it is referenced
- Definitions is stored in factory, instances are not
- Very useful for transient data or types that flex based on application
state
3)Session
- Applies to web environment only
- One instance of bean per user session
- Definition stored in bean factory, instance is not
4)Request
- Applies to web environment only
- One instance per request
- Definition stored in bean factory, instance is not
Below mentioned example shows configuring bean named "Worker" to prototype bean
@Bean
@Scope("prototype")
public Worker worker(){
return new Worker(greetingText);
}
1) Singleton
- The default scope of every bean is singleton.
- One instance per context definition
- Be careful with static data
2) Prototype
- New instance every time it is referenced
- Definitions is stored in factory, instances are not
- Very useful for transient data or types that flex based on application
state
3)Session
- Applies to web environment only
- One instance of bean per user session
- Definition stored in bean factory, instance is not
4)Request
- Applies to web environment only
- One instance per request
- Definition stored in bean factory, instance is not
Below mentioned example shows configuring bean named "Worker" to prototype bean
@Bean
@Scope("prototype")
public Worker worker(){
return new Worker(greetingText);
}
No comments:
Post a Comment