Spring Bean Scope (作用域)
singleton:
单例模式,针对每个spring容器,只有一个该类的实例被管理,每次调用此实例都是同一个对象被返回,所以适用于无状态bean。默认情况下,singleton作为spring容器中bean的作用域。
<bean id="accountService" class="com.foo.DefaultAccountService"/> <!-- the following is equivalent, though redundant (singleton scope is the default); using spring-beans-2.0.dtd -->
<bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/> <!-- the following is equivalent and preserved for backward compatibility in spring-beans.dtd -->
<bean id="accountService" class="com.foo.DefaultAccountService" singleton="true"/>
prototype:
针对每一次bean调用,注入或者程序中显式调用getBean(...)类似方法,都有一个新的对象被初始化后返回,所以适用于有状态bean。值得注意的是尽管初始化回调方法依然会被调用,但是声明为prototype的bean的“销毁”回调方法不会被容器调用。spring container初始装配之后将控制权交给客户代码,客户代码需要承担释放资源的责任。(spring 提供prototype资源的释放方案,BeanPostProcessors)。
<!-- using spring-beans-2.0.dtd -->
<bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/> <!-- the following is equivalent and preserved for backward compatibility in spring-beans.dtd -->
<bean id="accountService" class="com.foo.DefaultAccountService" singleton="false"/>
request,session,global session:
以上三种顾名思义,作用域分别是http request级别,session级别。spring context需要是web实现(比如:XmlWebApplicationContext)。DispatcherServlet/DispatcherPortlet,RequestContextListener 或RequestContextFilter 负责将每个http request/session 绑定到负责相应这个请求的线程上,并使得声名为request/session作用域的bean在后续调用中可用。
spring mvc
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
servlet 2.4+
<web-app>
...
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>
servlet 2.3
<web-app>
..
<filter>
<filter-name>requestContextFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>requestContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
...
</web-app>
refer to spring reference
Spring Bean Scope (作用域)的更多相关文章
- spring中scope作用域(转)
今天研究了一下scope的作用域.默认是单例模式,即scope="singleton".另外scope还有prototype.request.session.global sess ...
- Spring Bean的作用域(转)
Spring Bean的作用域 .singleton [单例] eg:<bean id="personService" class="com.yinger.ser ...
- Spring bean的作用域以及生命周期
一.request与session的区别 request简介 request范围较小一些,只是一个请求. request对象的生命周期是针对一个客户端(说确切点就是一个浏览器应用程序)的一次请求,当请 ...
- spring bean 的作用域
spring bean 的作用域: 1.单例(singleton):默认是单例模式,也就是说不管给定的bean被注入到其他bean多少次,注入的都是同一个实例. 2.原型(prototype):每次注 ...
- spring bean 的作用域之间有什么区别
spring bean 的作用域之间有什么区别? spring容器中的bean可以分为五个范围.所有范围的名称都是说明的, 1.singleton:这种bean范围是默认的,这种范围确保不管接受到多个 ...
- Spring中scope作用域
scope作用域: 1.prototype 2.request 3.session 4.singleton 5.global session 1.prototype(多例) prototyp ...
- Spring bean的作用域和生命周期
bean的作用域 1.singleton,prototype, web环境下:request,session,gloab session 2.通过scope="" 来进行配置 3. ...
- [跟我学spring][Bean的作用域]
Bean的作用域 什么是作用域呢?即“scope”,在面向对象程序设计中一般指对象或变量之间的可见范围.而在Spring容器中是指其创建的Bean对象相对于其他Bean对象的请求可见范围. Sprin ...
- (转)Spring Bean Scope 有状态的Bean 无状态的Bean
有状态会话bean :每个用户有自己特有的一个实例,在用户的生存期内,bean保持了用户的信息,即“有状态”:一旦用户灭亡(调用结束或实例结束),bean的生命期也告结束.即每个用户最初都会得到一 ...
随机推荐
- 领扣(LeetCode)合并两个有序数组 个人题解
给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量分别为 m 和 n. ...
- bash:echo
echo 'xxxx'自带换行 echo -n ‘xxxxxx’ 取消换行 echo -e "xxxxxxxxxxxx"允许转义字符(两种引号对转以字符效果相同,影响$变量) 转义 ...
- Pashmak and Buses(构造)
题目链接:http://codeforces.com/problemset/problem/459/C 题意:n个人, k辆车, d天,每天将所有 任意人安排到k辆车, 问怎样安排, 可时不存在 2人 ...
- RHEL7.2 SSH无密码登录非root用户
1 修改三台虚拟机的/ect/hosts文件 [hadoop@hadoop01 ~]$ cat /etc/hosts 127.0.0.1 localhost localhost.localdomain ...
- 【algo&ds】8.最小生成树
1.最小生成树介绍 什么是最小生成树? 最小生成树(Minimum spanning tree,MST)是在一个给定的无向图G(V,E)中求一棵树T,使得这棵树拥有图G中的所有顶点,且所有边都是来自图 ...
- epoll介绍及使用
小程序功能:简单的父子进程之间的通讯,子进程负责每隔1s不断发送"message"给父进程,不需要跑多个应用实例,不需要用户输入. 首先上代码 #include<assert ...
- 如何编译和使用自定义Qt动态链接库 | how to build and use user-defined qt library
本文首发于个人博客https://kezunlin.me/post/cf628dd8/,欢迎阅读! guide to build qt library and use in another proje ...
- Linux考题(一)
1.创建目录/data/oldboy,并且在该目录下创建oldboy.txt,然后在文件oldybos.txt里写入内容“inet addr:192.168.228.128 Bcast:192.16 ...
- Python 编程语言要掌握的技能之一:善用变量来改善代码质量
如何为变量起名 在计算机科学领域,有一句著名的格言(俏皮话): There are only two hard things in Computer Science: cache invalidati ...
- Golang 指针理解
目录 0x00 指针地址和指针类型 0x01 从指针获取指针指向的值 0x02 使用指针修改值 0x03 返回函数中局部变量 0x04 使用 new() 创建指针 0x05 flag包的指针技术 0x ...