溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

springBean的作用域怎么實(shí)現(xiàn)

發(fā)布時間:2023-02-07 09:22:05 來源:億速云 閱讀:119 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“springBean的作用域怎么實(shí)現(xiàn)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“springBean的作用域怎么實(shí)現(xiàn)”吧!

如果想學(xué)習(xí)SpringBean的生命周期,那么就必須要學(xué)習(xí)Bean的作用域。因?yàn)椴煌淖饔糜虻腷ean的生命周期不同

1:singleton(唯一bean實(shí)例,由Spring容器管理其生命周期)
2:prototype(原型bean,創(chuàng)建后容器不管理其生命周期)
3:request(每次http都產(chǎn)生新的bean,僅在http request內(nèi)有效)
4:session(首次http請求創(chuàng)建一個實(shí)例,作用域是瀏覽器首次訪問直至瀏覽器關(guān)閉)
5:global-session(全局 session 作用域,僅僅在基于 Portlet 的 web 應(yīng)用中才有意義,Spring5 已經(jīng)沒有了。

  • singleton 是默認(rèn)的作用域,我們?nèi)绻粚ean的scope配置項(xiàng)進(jìn)行配置的話,默認(rèn)就是Singleton類型。 在創(chuàng)建起容器時就同時自動創(chuàng)建了一個 bean 的對象,不管你是否使用,他都存在了,每次獲取到的對象都是同一個對象。

  • prototype: 原型bean,每次對此類型的bean進(jìn)行請求的時候,都會創(chuàng)建一個新的bean實(shí)例。與我們 java中的new效果類似。Spring 只負(fù)責(zé)創(chuàng)建,當(dāng)容器創(chuàng)建了 Bean 的實(shí)例后,Bean 的實(shí)例就交給客戶端代碼管理,Spring 容器將不再跟蹤其生命周期。

  • request:每次HTTP請求都會創(chuàng)建一個新的Bean

  • session:首次http請求創(chuàng)建一個實(shí)例,作用域是瀏覽器首次訪問直至瀏覽器關(guān)閉

  • global-session:作用域范圍是WebApplicationContext中。一般用于Portlet應(yīng)用環(huán)境,該運(yùn)用域僅適用于WebApplicationContext環(huán)境。

后三種只有在web環(huán)境下才有效。

bean的作用域具體實(shí)現(xiàn)

我針對對前兩種作用域編寫了一個對應(yīng)的例子,這是一個普通的Maven項(xiàng)目,引進(jìn)了spring的包。首先看一下項(xiàng)目結(jié)構(gòu)

springBean的作用域怎么實(shí)現(xiàn)

1.空的AService類

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.tfjy.test
 * @Author: haolizhuo
 * @Description: A服務(wù)類
 * @CreateTime: 2023-01-28 09:59
 * @Version: 1.0
 */
@Component
//@Scope("prototype")
public class AService {

}

xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!--開啟注解的支持-->
    <context:annotation-config/>
    <!-- 自動掃描指定包及其子包下的所有Bean類 -->
    <context:component-scan base-package="com.tfjy.test"/>

    <!--    將AService設(shè)置為原型bean-->
<!--    <bean id="AService" class="com.tfjy.test.AService" scope="prototype"></bean>-->

</beans>

Test測試類

/**
 * @BelongsProject: demo
 * @BelongsPackage: PACKAGE_NAME
 * @Author: haolizhuo
 * @Description: test測試類
 * @CreateTime: 2023-01-28 10:01
 * @Version: 1.0
 */
public class Test {
    //bean驗(yàn)證
    @org.junit.Test
    public void beanTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

        AService aServiceOne = context.getBean("AService",AService.class);
        AService aServiceTwo = context.getBean("AService",AService.class);

        System.out.println(aServiceOne);
        System.out.println(aServiceTwo);
        //通過equals方法判斷兩個對象是否相等
        if(aServiceOne.equals(aServiceTwo)){
            System.out.println("兩次getBean方法,獲得了同一個單例對象");
        }else{
            System.out.println("兩次getBean方法,獲得的不是同一個單例對象");
        }
    }
}

4.pom文件引入的依賴

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.15.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

代碼分析

上述代碼中直接運(yùn)行的話,是沒有配置bean的作用域的。所以控制臺會打印,兩次getBean方法,獲得了同一個單例對象

springBean的作用域怎么實(shí)現(xiàn)

我們設(shè)置bean對象為prototype類型的方式也有兩種。
1.注解方式。
在需要交由IOC容器管理的bean對象類上面添加@Scope(“prototype”)注解。
2.xml配置文件方式

<bean id="AService" class="com.tfjy.test.AService" scope="prototype"></bean>

這兩種方式設(shè)置任意一種,spring加載bean的時候都會去讀取配置,并將對應(yīng)bean設(shè)置為prototype類型。

springBean的作用域怎么實(shí)現(xiàn)

到此,相信大家對“springBean的作用域怎么實(shí)現(xiàn)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI