最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談Spring Bean的作用域之間有什么區(qū)別

 更新時間:2024年05月12日 10:54:48   作者:逆流°只是風(fēng)景-bjhxcc  
Spring的bean有5種作用域是singleton、prototype、request、session和globalSession,本文主要介紹了淺談Spring Bean的作用域之間有什么區(qū)別,感興趣的可以了解一下

前言

Spring的 bean有5種作用域分別是:singleton、prototype、request、session和globalSession

Spring Bean的作用域之間有什么區(qū)別?

? 在Spring中,可以在<bean>元素的scope屬性里設(shè)置bean的作用域,以決定這個bean是單例的還是多例的。

? 默認(rèn)情況下,Spring只為每個在IOC容器里聲明的bean創(chuàng)建唯一一個實例,整個IOC容器范圍內(nèi)都能共享該實例:所有后續(xù)的getBean()調(diào)用和bean引用都將返回這個唯一的bean實例。該作用域稱為singleton,它是bean的默認(rèn)作用域。

作用域的類別跟說明

  • singleton:在SpringIOC容器中僅存在一個Bean實例,Bean以單實例的方式存在
  • prototype:每次調(diào)用getBean()時都會返回一個新的實例
  • request:每次HTTP請求都會創(chuàng)建一個新的Bean,該作用域僅適用于WebApplicationContext環(huán)境
  • session:同一個HTTP Session共享一個Bean,不同的HTTP Session使用不同的Bean,該作用域僅適用于WebApplicationContext環(huán)境

簡述

bean的作用域:可以通過元素的scope屬性來指定bean作用域

  • singleton:默認(rèn)值。當(dāng)IOC容器一創(chuàng)建就會創(chuàng)建bean的實例,而且是單例的,每次得到的都是同一個
  • prototype:原型的。當(dāng)IOC容器一創(chuàng)建不再實例化該bean,每次調(diào)用getBean方法時再實例化該bean,而且每調(diào)用
  • request:每次請求實例化一個bean
  • session:在一次會話中共享一個bean

測試用例

因為平時使用SPRING MVC開發(fā)的時候比較多,有必要了解清楚怎么去調(diào)用這幾種作用域。

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是單例的,因此必須通過實現(xiàn)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. 運行結(jié)果

//使用chrome第一次打印數(shù)據(jù):  
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打印第二次數(shù)據(jù)  
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打印第三次數(shù)據(jù)  
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.結(jié)果分析

從結(jié)果來看,單例的bean的三次的數(shù)據(jù)都是打印一樣的(默認(rèn)的bean的級別就是單例);
prototype的bean每次的數(shù)據(jù)都是不一樣的,每次請求的時候調(diào)用兩次結(jié)果都不一樣。
request的bean在每次request的時候都不一致,但是同一次request返回的數(shù)據(jù)是一致的。
session的bean在前兩次結(jié)果一致,最后一次數(shù)據(jù)不一致,和session的節(jié)奏是一致的。

5. 欠缺

網(wǎng)絡(luò)上說必需配置

<listener>   
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>   
</listener>   

但是沒配置也好使……好奇……

最后一種作用域是適用于portlet,沒試驗,據(jù)說是在多個session之間可以共享,效果等同于全局變量。

到此這篇關(guān)于淺談Spring Bean的作用域之間有什么區(qū)別的文章就介紹到這了,更多相關(guān)Spring Bean作用域內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Hutool開發(fā)MapUtil工具類使用示例

    Hutool開發(fā)MapUtil工具類使用示例

    這篇文章主要為大家介紹了Hutool開發(fā)MapUtil工具類使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • JDK9為何要將String的底層實現(xiàn)由char[]改成了byte[]

    JDK9為何要將String的底層實現(xiàn)由char[]改成了byte[]

    String 類的源碼已經(jīng)由?char[]?優(yōu)化為了?byte[]?來存儲字符串內(nèi)容,為什么要這樣做呢?本文就詳細(xì)的介紹一下,感興趣的可以了解一下
    2022-03-03
  • Java中使用qsort對類進行排序的操作代碼

    Java中使用qsort對類進行排序的操作代碼

    這篇文章主要介紹了JAVA中如何使用qsort對類進行排序,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09
  • springboot中添加靜態(tài)頁面的三種實現(xiàn)方案與對比

    springboot中添加靜態(tài)頁面的三種實現(xiàn)方案與對比

    這篇文章主要為大家詳細(xì)介紹了springboot中添加靜態(tài)頁面的三種實現(xiàn)方案與對比,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-11-11
  • 淺談Java中SimpleDateFormat 多線程不安全原因

    淺談Java中SimpleDateFormat 多線程不安全原因

    SimpleDateFormat是Java中用于日期時間格式化的一個類,本文主要介紹了淺談Java中SimpleDateFormat 多線程不安全原因,感興趣的可以了解一下
    2024-01-01
  • SpringBoot沒有主清單屬性的解決方法

    SpringBoot沒有主清單屬性的解決方法

    在本篇文章里小編給大家整理的是關(guān)于解決SpringBoot沒有主清單屬性知識點,需要的朋友們學(xué)習(xí)下。
    2019-11-11
  • SpringBoot整合MybatisPlus實現(xiàn)增刪改查功能

    SpringBoot整合MybatisPlus實現(xiàn)增刪改查功能

    MybatisPlus是國產(chǎn)的第三方插件,?它封裝了許多常用的CURDapi,免去了我們寫mapper.xml的重復(fù)勞動。本文將整合MybatisPlus實現(xiàn)增刪改查功能,感興趣的可以了解一下
    2022-05-05
  • SpringBoot之spring.factories的使用方式

    SpringBoot之spring.factories的使用方式

    這篇文章主要介紹了SpringBoot之spring.factories的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Spring中的集合注入代碼實例

    Spring中的集合注入代碼實例

    這篇文章主要介紹了Spring中的集合注入代碼實例,集合注入是指在Spring框架中,通過配置文件或注解的方式將集合類型的數(shù)據(jù)注入到Bean中,集合類型包括List、Set、Map和Properties等,需要的朋友可以參考下
    2023-11-11
  • 解析SpringBoot @EnableAutoConfiguration的使用

    解析SpringBoot @EnableAutoConfiguration的使用

    這篇文章主要介紹了解析SpringBoot @EnableAutoConfiguration的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09

最新評論

沧州市| 鲜城| 利川市| 湖南省| 翼城县| 长寿区| 马鞍山市| 清涧县| 萝北县| 汤阴县| 正定县| 前郭尔| 年辖:市辖区| 徐闻县| 铜鼓县| 长顺县| 香河县| 浦县| 汾西县| 休宁县| 孝义市| 精河县| 兴海县| 东台市| 鄂伦春自治旗| 满城县| 普宁市| 青岛市| 双牌县| 卢湾区| 双牌县| 融水| 雷州市| 武义县| 鸡东县| 嘉善县| 鄄城县| 宜兰县| 屯留县| 双牌县| 冷水江市|