学习笔记:Spring Bean的作用域

  Spring Framework支持五种作用域(其中有三种只能用在基于web的Spring ApplicationContext)。
这里写图片描述

singleton

singleton: This bean scope is default and it enforces the container to have only one instance per spring container irrespective of how much time you request for its instance. This singleton behavior is maintained by bean factory itself.

  singleton作用域是Spring中的缺省作用域,当一个bean的作用域为singleton, 那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。singleton作用域是有Bean工厂维护。

prototype

prototype: This bean scope just reverses the behavior of singleton scope and produces a new instance each and every time a bean is requested.

  prototype作用域与singleton作用域相反,为每一个bean请求提供一个实例。


Remaining three bean scopes are web applications related. Essentially these are available through web aware application context (e.g. WebApplicationContext). Global-session is a little different in sense that it is used when application is portlet based. In portlets, there will be many applications inside a big application and a bean with scope of ‘global-session’ will have only one instance for a global user session.

  其余三个作用域是基于web application的。实质上它们是基于web application上下文(例如: WebApplicationContext)才能有效。Global-session在使用过程中有所不同,在portlet容器中,在一个大的工程中往往有很多portlet容器,而global-session保证在所有的portlet会话中只有一个实例。
  

request

With this bean scope, a new bean instance will be created for each web request made by client. As soon as request completes, bean will be out of scope and garbage collected.

  当一个bean被定义为request作用域时,会对每一个客户端请求创建一个实例,在请求完成以后,bean会失效并被垃圾回收器回收。

session

Just like request scope, this ensures one instance of bean per user session. As soon as user ends its session, bean is out of scope.

  与request作用域类似,session作用域确保每个session中有一个bean的实例,在session过期后,bean会随之失效。

global-session

global-session is something which is connected to Portlet applications. When your application works in Portlet container it is built of some amount of portlets. Each portlet has its own session, but if your want to store variables global for all portlets in your application than you should store them in global-session. This scope doesn’t have any special effect different from session scope in Servlet based applications.

  global-session和Portlet应用相关。当你的应用部署在Portlet容器中工作时,它包含很多portlet。如果你想要声明让所有的portlet共用全局的存储变量的话,那么这全局变量需要存储在global-session中。

定义Bean作用域的方式

(1)配置文件中定义

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

       <bean id="demoBean" class="com.howtodoinjava.application.web.DemoBean" scope="session" />

</beans>

(2)注解定义

@Service
@Scope("session")
public class DemoBean 
{
    //Some code
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值