Spring_bean Scope

This introduction scope Spring Bean instance, Spring Bean scope by the example of the configuration item defined scope. By Benpian study, you can reach the following objectives.

● application scope configuration items Configuration Bean scopes

● application singleton pattern singleton

● application prototype model prototype

 

1. Scope scope configuration items

Scope defines the scope of the Spring Bean, in the Spring configuration file defines Bean, by declaring scope configuration items, you can flexibly define the scope of Bean. For example, when you want to return each time the IOC container Bean is the same instance, you can set the scope as a singleton; when you want every instance of Bean IOC container returns a new instance, you can set the scope for the prototype.

CI scope has five attributes used to describe different scopes.

① singleton

When this attribute defines Bean, IOC container creates only a Bean instance, IOC each container returned to the same Bean instance.

② prototype

When this attribute defines Bean, Bean the IOC container may create multiple instances, each instance of a new one is returned.

③ request

The property is only HTTP requests have an effect, when using the attribute definition Bean, each HTTP request will create a new Bean, suitable for WebApplicationContext environment.

④ session

This attribute is only used for HTTP Session, a Session sharing a same Bean instance. Different Session different instance.

⑤ global-session

This attribute is only used for HTTP Session, the same session scope is different, all share a Session Bean instance.

Focuses on the following singleton, prototyp scope, request, session, and global-session class scope into sections discuss Spring MVC, do not describe in detail here.

Guess you like

Origin www.cnblogs.com/xiaozhang666/p/11606549.html