Spring Bean Scope

发布于:2024-05-11 ⋅ 阅读:(124) ⋅ 点赞:(0)

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.

Scope describe
singleton There is only one bean instance in the spring IoC container. The bean exists as a single instance.
prototype Every 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()
request Each HTTP request will create a new bean. This scope is only applicable to the WebApplicationContext environment
session The same HTTP session shares a bean, and different sessions use different beans, which is only applicable to the WebApplicationContext environment
global-session It is generally used in the portlet application environment, which is only applicable to the WebApplicationContext environment

网站公告

今日签到

点亮在社区的每一天
去签到