前言
spring的 bean有5种作用域分别是:singleton、prototype、request、session和globalsession
spring bean的作用域之间有什么区别?
在spring中,可以在<bean>元素的scope属性里设置bean的作用域,以决定这个bean是单例的还是多例的。
默认情况下,spring只为每个在ioc容器里声明的bean创建唯一一个实例,整个ioc容器范围内都能共享该实例:所有后续的getbean()调用和bean引用都将返回这个唯一的bean实例。该作用域称为singleton,它是bean的默认作用域。
作用域的类别跟说明
- singleton:在springioc容器中仅存在一个bean实例,bean以单实例的方式存在
- prototype:每次调用getbean()时都会返回一个新的实例
- request:每次http请求都会创建一个新的bean,该作用域仅适用于webapplicationcontext环境
- session:同一个http session共享一个bean,不同的http session使用不同的bean,该作用域仅适用于webapplicationcontext环境
简述
bean的作用域:可以通过元素的scope属性来指定bean作用域
- singleton:默认值。当ioc容器一创建就会创建bean的实例,而且是单例的,每次得到的都是同一个
- prototype:原型的。当ioc容器一创建不再实例化该bean,每次调用getbean方法时再实例化该bean,而且每调用
- request:每次请求实例化一个bean
- session:在一次会话中共享一个bean
测试用例
因为平时使用spring mvc开发的时候比较多,有必要了解清楚怎么去调用这几种作用域。
1. 定义不同作用域的java类
@component @scope( "session") public class sessionobj { } @component @scope( "request") public class requestobj { } @component @scope( "prototype") public class prototypeobj { } @component @scope( "singleton") public class singletonobj { }
2. 注入到controller,由于controller是单例的,因此必须通过实现applicationcontextaware接口,直接从容器中取出对象。
因此测试的controller是:
@controller public class indexcontroller implements applicationcontextaware { private requestobj requestobj; private sessionobj sessionobj; private prototypeobj prototypeobj; private singletonobj singletonobj; private applicationcontext applicationcontext; @requestmapping("/") @responsebody public string index() { print(); return "welcome"; } public void print() { system.out.println("first time singleton is :" + getsingletonobj()); system.out.println("second time singleton is :" + getsingletonobj()); system.out.println("first time prototype is :" + getprototypeobj()); system.out.println("second time prototype is :" + getprototypeobj()); system.out.println("first time request is :" + getrequestobj()); system.out.println("second time request is :" + getrequestobj()); system.out.println("first time session is :" + getsessionobj()); system.out.println("second time session is :" + getsessionobj()); } @override public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception { this.applicationcontext = applicationcontext; } public requestobj getrequestobj() { return applicationcontext.getbean(requestobj.class); } public void setrequestobj(requestobj requestobj) { requestobj = requestobj; } public sessionobj getsessionobj() { return applicationcontext.getbean(sessionobj.class); } public void setsessionobj(sessionobj sessionobj) { sessionobj = sessionobj; } public prototypeobj getprototypeobj() { return applicationcontext.getbean(prototypeobj.class); } public void setprototypeobj(prototypeobj prototypeobj) { prototypeobj = prototypeobj; } public singletonobj getsingletonobj() { return applicationcontext.getbean(singletonobj.class); } public void setsingletonobj(singletonobj singletonobj) { singletonobj = singletonobj; } }
3. 运行结果
//使用chrome第一次打印数据: first time singleton is :com.fb.po.singletonobj@1e3223e second time singleton is :com.fb.po.singletonobj@1e3223e first time prototype is :com.fb.po.prototypeobj@3e683f second time prototype is :com.fb.po.prototypeobj@12e18d7 first time request is :com.fb.po.requestobj@1d45706 second time request is :com.fb.po.requestobj@1d45706 first time session is :com.fb.po.sessionobj@9a6b2e second time session is :com.fb.po.sessionobj@9a6b2e //使用chrome打印第二次数据 first time singleton is :com.fb.po.singletonobj@1e3223e second time singleton is :com.fb.po.singletonobj@1e3223e first time prototype is :com.fb.po.prototypeobj@122e5be second time prototype is :com.fb.po.prototypeobj@192add first time request is :com.fb.po.requestobj@4d1b6c second time request is :com.fb.po.requestobj@4d1b6c first time session is :com.fb.po.sessionobj@9a6b2e second time session is :com.fb.po.sessionobj@9a6b2e //使用ie打印第三次数据 first time singleton is :com.fb.po.singletonobj@1e3223e second time singleton is :com.fb.po.singletonobj@1e3223e first time prototype is :com.fb.po.prototypeobj@10f1ecb second time prototype is :com.fb.po.prototypeobj@1aeb990 first time request is :com.fb.po.requestobj@18a1e7 second time request is :com.fb.po.requestobj@18a1e7 first time session is :com.fb.po.sessionobj@12d5c55 second time session is :com.fb.po.sessionobj@12d5c55
4.结果分析
从结果来看,单例的bean的三次的数据都是打印一样的(默认的bean的级别就是单例);
prototype的bean每次的数据都是不一样的,每次请求的时候调用两次结果都不一样。
request的bean在每次request的时候都不一致,但是同一次request返回的数据是一致的。
session的bean在前两次结果一致,最后一次数据不一致,和session的节奏是一致的。
5. 欠缺
网络上说必需配置
<listener> <listener-class>org.springframework.web.context.request.requestcontextlistener</listener-class> </listener>
但是没配置也好使……好奇……
最后一种作用域是适用于portlet,没试验,据说是在多个session之间可以共享,效果等同于全局变量。
到此这篇关于浅谈spring bean的作用域之间有什么区别的文章就介绍到这了,更多相关spring bean作用域内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论