The spring bean's scope

The spring bean's scope




spring容器中的bean默认“单实例”的,即不管你调用 getBean() 方法多少次,得到的始终是一个bean实例。


Spring provides us with beans after instantiating and coniguring them. Spring
Container manages objects. This means that any object can refer to any other object from Spring Container using the bean's ID, and Spring Container provides an instance of the requesting object.
When we start Spring Container, ApplicationContext reads the Spring coniguration,file looks for all bean deinitions available there, and then initializes beans before any call to the getBean() method.
During initialization, ApplicationContext itself has initialized all the Spring beans conigured in Spring XML. When another object makes a call to the getBean() method, ApplicationContext returns the same reference of bean that has already been initialized. This is the default behavior of beans.


Singleton

By default, all Spring beans are singleton. Once ApplicationContext is initialized,
it looks at all the beans in XML and initializes only one bean per bean deinition in
Spring Container. On each call to the getBean() method, Spring Container returns
the same instance of the bean.
The first bean scope in Spring that is called is singleton, which initializes only one
bean per bean deinition in the container and returns the same instance reference
on each call to the getBean() method. This scope makes Spring initialize all beans
during the load time itself without waiting for the getBean() call.


当我们讨论单实例模式时候,一般要涉及到jvm实例。不过,在spring中,单实例就被限制在了spring容器内了。
当然,一个jvm实例可以运行多个spring容器,所以一个jvm实例内可以有一个bean的多个实例。
The singleton pattern in general says that overall there will be only one instance of
the object. But when we talk about singleton in the Spring Framework, we are talking
about Spring Container alone.
We can have multiple containers running in the same JVM, so we can have multiple
instances of the same bean in same JVM.
So, singleton in Spring represents in a particular Spring container, and there is only
one instance of a bean created in that container that is used across different references.


Prototype



原型模式,每次获取的bean实例都是全新的。
The prototype is second bean scope in Spring, which returns a brand-new instance of
a bean on each call to the getBean() method. When a bean is deined as a prototype,
Spring waits for getBean() to happen and only then does it initialize the prototype.
For every getBean() call, Spring has to perform initialization, so instead of doing
default initialization while a context is being created, it waits for a getBean() call.
So, every time getBean() gets called, it creates a new instance.


Request

The third bean scope in Spring is request, which is available only in web applications
that use Spring and create an instance of bean for every HTTP request. Here, a new
bean is created per Servlet request. Spring will be aware of when a new request is
happening because it ties well with the Servlet APIs, and depending on the request,
Spring creates a new bean. So, if the request scope has getBean() inside it, for every
new request, there will be a new bean. However, as long as it's in the same request
scope, the same bean is going to be used.

Session

The session is the fourth bean scope in Spring, which is available only in web
applications that use Spring and create an instance of bean for every HTTP session.
Here, a new bean is created per session. As long as there is one user accessing in a
single session, each call to getBean() will return same instance of the bean. But if
it's a new user in a different session, then a new bean instance is created.

Global session

The global session is the ifth bean scope in Spring, which works only in portlet
environments that use Spring and create a bean for every new portlet session.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dreamer who

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值