The following allows the declaration of a singleton bean in Spring 3.0:
@Bean
@Scope(BeanDefinition.SCOPE_SINGLETON)
private void setBean1(Bean1 b1) {
this.b1 = b1;
}
But, BeanDefinition
does not define scopes values for request, session and global session. Where are these defined? Else, should I use @Scope("request")
, @Scope("session")
and @Scope("global session")
?
Session: Scopes a single bean definition to the lifecycle of an HTTP Session. But Only valid in the context of a web-aware Spring ApplicationContext. Global-Session: Scopes a single bean definition to the lifecycle of a global HTTP Session. It is also only valid in the context of a web-aware Spring ApplicationContext.
When used as a type-level annotation in conjunction with @Component , @Scope indicates the name of a scope to use for instances of the annotated type. When used as a method-level annotation in conjunction with @Bean , @Scope indicates the name of a scope to use for the instance returned from the method.
A scope defines the runtime context within which the bean instance is available. In Spring, a bean can be associated with the following scopes: Singleton. Prototype. Request.
6. Which of the following are considered valid beans? Explanation: In Spring 1. x, singleton and prototype are the only two valid bean scopes, and they are specified by the singleton attribute (i.e., singleton=”true” or singleton=”false”), not the scope attribute.
BeanDefinition
only has SCOPE_SINGLETON
and SCOPE_PROTOTYPE
.
The other scopes, being only applicable to web applications, are defined in org.springframework.web.context.WebApplicationContext
.
A useful list of constants can be found in the javadoc for Constant Field values.
You can use string literals if you want (though global session would be "globalSession").
Alternatively, you can use constants defined in WebApplicationContext
.
There is no single place, where all the scope names are defined, and not all scope names have constants:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With