您的位置:首页 > 编程语言 > Java开发

The spring bean's scope

2016-02-08 21:54 513 查看

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: