spring scope
Spring定义的scope包含五种,singleton,prototype,request,session,global session,每种scope都有自己的使用范围以及使用场景。
singleton
Scopes a single bean definition to a single object instance per Spring IoC container.
个人理解:在Spring 的IOC容器中,全局只会存在一个对象实例。每次spring启动之后,就会生成一个,也只会有一个唯一的bean对象
prototype
Scopes a single bean definition to any number of object instances.
个人理解:有无数个对象的实例,也就是说每个上层在引用时,都是一个独立的对象
request
Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring
ApplicationContext
个人理解:在每个http请求的生成周期内,使用同一个对象的实例,不同的http之间使用的对象都是不同的实例。只有在使用了spring的ApplicationContext
的上下文才有效
session
Scopes a single bean definition to the lifecycle of a HTTP
Session
,Only valid in the context of a web-aware SpringApplicationContext
个人理解:在同一个session的生命周期内,使用同一个对象的实例
global session
Scopes a single bean definition to the lifecycle of a global HTTP
Session
. Typically only valid when used in a portlet context. Only valid in the context of a web-aware SpringApplicationContext
个人理解:在同一个全局session的生命周期内,使用同一个对象的实例,跟上面的session的区别在于global,也就是全局,例如使用redis等分布式session的场景。