浅谈Spring Bean的作用域之间有什么区别

 更新时间:2024年05月12日 10:54:48   作者:逆流°只是风景-bjhxcc  
Spring的bean有5种作用域是singleton、prototype、request、session和globalSession,本文主要介绍了浅谈Spring Bean的作用域之间有什么区别,感兴趣的可以了解一下

前言

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作用域内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • springboot使用redisTemplate操作lua脚本

    springboot使用redisTemplate操作lua脚本

    本文主要介绍了springboot使用redisTemplate操作lua脚本,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • Java中多种循环Map的常见方式详解

    Java中多种循环Map的常见方式详解

    Java中的Map是一种键值对存储的数据结构,其中每个键都唯一,与一个值相关联,下面这篇文章主要给大家介绍了关于Java中多种循环Map的常见方式,文中给出了详细的代码示例,需要的朋友可以参考下
    2024-01-01
  • SpringMVC配置多个properties文件之通配符解析

    SpringMVC配置多个properties文件之通配符解析

    这篇文章主要介绍了SpringMVC配置多个properties文件之通配符解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • Spring中的EventListenerMethodProcessor组件详解

    Spring中的EventListenerMethodProcessor组件详解

    这篇文章主要介绍了Spring中的EventListenerMethodProcessor组件详解,EventListenerMethodProcessor 是 Spring 事件机制中非常重要的一个组件,它管理了一组EventListenerFactory组件,用来将应用中每个使用@EventListener注解定义的事件监听,需要的朋友可以参考下
    2023-12-12
  • Spring Security基于数据库实现认证过程解析

    Spring Security基于数据库实现认证过程解析

    这篇文章主要介绍了Spring Security基于数据库实现认证过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • Java中如何判断是否为闰年详细实例

    Java中如何判断是否为闰年详细实例

    所谓闰年就是指2月有29天的那一年,下面这篇文章主要给大家介绍了关于Java中如何判断是否为闰年的相关资料,文中通过图文以及实例代码介绍的非常详细,需要的朋友可以参考下
    2023-06-06
  • java实现图片上传至本地实例详解

    java实现图片上传至本地实例详解

    我们给大家分享了关于java实现图片上传至本地的实例以及相关代码,有需要的朋友参考下。
    2018-08-08
  • MyBatis 中 SqlMapConfig 配置文件详解

    MyBatis 中 SqlMapConfig 配置文件详解

    这篇文章主要介绍了MyBatis 中 SqlMapConfig 配置文件详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • SpringBoot使用Validation进行参数校验的示例详解

    SpringBoot使用Validation进行参数校验的示例详解

    在 SpringBoot项目开发中,有一个观点是不要相信前端传入的参数,因为你不知道用户是怎么操作我们接口的,所以在后端也需要对参数进行校验,这篇文章主要讲讲我们项目中最常使用的验证方案
    2023-05-05
  • Spring集成Redis详解代码示例

    Spring集成Redis详解代码示例

    这篇文章主要介绍了Spring集成Redis详解代码示例,介绍了Eclipse工程结构,POM依赖,Spring配置,Redis配置信息以及Java代码等相关内容,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11

最新评论