欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

淺談springBean的作用域

 更新時間:2023年02月05日 12:16:10   作者:郝老三  
本文主要介紹了淺談springBean的作用域,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言:最近在進行springBean的作用域?qū)W習(xí),并且學(xué)習(xí)了對應(yīng)的例子。這里進行一下總結(jié) 一:Bean的作用域基礎(chǔ)概念

如果想學(xué)習(xí)SpringBean的生命周期,那么就必須要學(xué)習(xí)Bean的作用域。因為不同的作用域的bean的生命周期不同

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

  • singleton 是默認(rèn)的作用域,我們?nèi)绻粚ean的scope配置項進行配置的話,默認(rèn)就是Singleton類型。 在創(chuàng)建起容器時就同時自動創(chuàng)建了一個 bean 的對象,不管你是否使用,他都存在了,每次獲取到的對象都是同一個對象。
  • prototype: 原型bean,每次對此類型的bean進行請求的時候,都會創(chuàng)建一個新的bean實例。與我們 java中的new效果類似。Spring 只負(fù)責(zé)創(chuàng)建,當(dāng)容器創(chuàng)建了 Bean 的實例后,Bean 的實例就交給客戶端代碼管理,Spring 容器將不再跟蹤其生命周期。
  • request:每次HTTP請求都會創(chuàng)建一個新的Bean
  • session:首次http請求創(chuàng)建一個實例,作用域是瀏覽器首次訪問直至瀏覽器關(guān)閉
  • global-session:作用域范圍是WebApplicationContext中。一般用于Portlet應(yīng)用環(huán)境,該運用域僅適用于WebApplicationContext環(huán)境。

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

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

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

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驗證
    @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>

代碼分析

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

我們設(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類型。

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

相關(guān)文章

最新評論