当前位置:   article > 正文

Spring Bean Scope

Spring Bean Scope

Bean Scope

When defining a bean in Spring, you must declare the scope options of the bean. For example, to force Spring to generate a new bean instance every time it needs to, you should declare the attribute of the bean’s scope as prototype. Similarly, if you want Spring to return the same bean instance every time you need it, you should declare the scope property of the bean as singleton.
The Spring framework supports the following five scopes. If you use web aware ApplicationContext, three of them are available.

Scopedescribe
singletonThere is only one bean instance in the spring IoC container. The bean exists as a single instance.
prototypeEvery time a bean is called from the container, a new instance is returned, that is, every time getBean() is called, it is equivalent to executing newXxxBean()
requestEach HTTP request will create a new bean. This scope is only applicable to the WebApplicationContext environment
sessionThe same HTTP session shares a bean, and different sessions use different beans, which is only applicable to the WebApplicationContext environment
global-sessionIt is generally used in the portlet application environment, which is only applicable to the WebApplicationContext environment
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/540195
推荐阅读
相关标签
  

闽ICP备14008679号