spring bean in five scopes? Spring is in the bean thread-safe yet? Spring Bean in five scopes

spring bean in five scopes

When you create a Bean instance by spring container can not only complete example of the Bean instance, Bean can also specify a particular scope. Spring supports the following five kinds of scopes:

  • singleton: Singleton, the entire Spring IoC container, using the singleton Bean definitions only one instance

  • prototype: Prototype mode, every acquisition Bean defined by the prototype method getBean container, will produce a new instance of the Bean

  • request: For each HTTP request using the request Bean will have defined a new instance, i.e. each HTTP request will produce different Bean instance. Only use in Spring Web applications, this scope is valid

  • session: For each HTTP Session, defined using the session Bean milk creates a new instance. Likewise use Spring only in Web applications, this scope is valid

  • globalsession: each global HTTP Session, Bean defined using the session will produce a new instance. Typically, the only effective when using the portlet context. Likewise use Spring only in Web applications, this scope is valid

  The more commonly used two kinds of singleton and prototype scopes. For singleton scope of Bean, Bean each request will receive the same instance. Bean container is responsible for tracking state instances, Bean instance is responsible for maintaining the life cycle of behavior; if a prototype is set to Bean scope, the id of each program requests Bean, Spring will create a new Bean instance, and then returns to the program. In this case, Spring container just created using the new keyword Bean instance, once created, the container is not tracking instance, will not maintain state Bean instance.

  If you do not specify Bean scopes, Spring use singleton scope by default. When creating Java Java instance, the need for memory allocation; when the instance is destroyed, garbage collection needs to be done, these efforts will lead to increased overhead. Therefore, prototype creation Bean scope of the destruction costs is relatively large. The Bean instance singleton scope once created, can be reused. Therefore, unless necessary to avoid Bean will be set to prototype scope.

 

Reference links: the Spring Bean in five scopes

 

Spring is in the bean thread-safe?

Bean thread-safe management with the spring race conditions if the bean and bean create a scope where the environment related to the presence, spring does not guarantee thread-safe bean.

 

Reference links: the Spring of bean thread-safe?

Guess you like

Origin www.cnblogs.com/jxxblogs/p/12147414.html
Recommended