(四)測試學習JavaWeb之Spring Bean作用域

前言

Bean的作用域是指spring容器從創(chuàng)建Bean到銷毀的整個過程。Spring容器創(chuàng)建的bean默認是singleton單例模式,即每個Bean的實例只創(chuàng)建一次。Spring可以為Bean指定以下五種作用域。

  • singleton,每個Bean的實例只創(chuàng)建一次。
  • prototype,每次獲取Bean實例時都會新創(chuàng)建一個實例對象。
  • request,對于每次HTTP請求,Spring容器都會創(chuàng)建一個Bean實例,整個請求過程使用相同的bean實例。不同的請求創(chuàng)建新的實例。
  • session,對于HTTP Session,每次會創(chuàng)建一個Session作用域的Bean。
  • globalsession,每個全局的HTTP Session,使用session定義的Bean都將產(chǎn)生一個新實例。

舉例說明

新建類,并指定作用域。

package example.bean;

import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;

@Component
@Scope(value = "prototype",proxyMode = ScopedProxyMode.TARGET_CLASS)
public class PrototypeBean {
}

package example.bean;

import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;

@Component
@Scope(value = "request",proxyMode = ScopedProxyMode.TARGET_CLASS)
public class RequestBean {
}

package example.bean;

import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;

@Component
@Scope(value = "session",proxyMode = ScopedProxyMode.TARGET_CLASS)
public class SessionBean {
}

package example.bean;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(value = "singleton")
public class SingleBean {
}

新建controller類

package example.controller;

import example.bean.PrototypeBean;
import example.bean.RequestBean;
import example.bean.SessionBean;
import example.bean.SingleBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class BeanTestController {

    @Autowired
    private SingleBean singleBean;
    @Autowired
    private PrototypeBean prototypeBean;
    @Autowired
    private RequestBean requestBean;
    @Autowired
    private SessionBean sessionBean;

    @RequestMapping(value = "/tom",method = RequestMethod.GET)
    public void test(){
        print();
    }

    public void print(){
        System.out.println("singleBean:"+singleBean);
        System.out.println("singleBean:"+singleBean);
        System.out.println("prototypeBean:"+prototypeBean);
        System.out.println("prototypeBean:"+prototypeBean);
        System.out.println("requestBean:"+requestBean);
        System.out.println("requestBean:"+requestBean);
        System.out.println("sessionBean:"+sessionBean);
        System.out.println("sessionBean:"+sessionBean);
        System.out.println("***********************************");
    }
}

添加掃描包路徑至配置文件

<context:component-scan base-package="example.bean"/>

啟動Tomcat服務器,輸入http://localhost:8082/tom,連續(xù)運行兩次,觀察輸出結(jié)果如下。

singleBean:example.bean.SingleBean@172b39f
singleBean:example.bean.SingleBean@172b39f
prototypeBean:example.bean.PrototypeBean@cfb48d
prototypeBean:example.bean.PrototypeBean@c27128
requestBean:example.bean.RequestBean@12bcc60
requestBean:example.bean.RequestBean@12bcc60
sessionBean:example.bean.SessionBean@186a259
sessionBean:example.bean.SessionBean@186a259
***********************************
singleBean:example.bean.SingleBean@172b39f
singleBean:example.bean.SingleBean@172b39f
prototypeBean:example.bean.PrototypeBean@16dad90
prototypeBean:example.bean.PrototypeBean@1aa5e8d
requestBean:example.bean.RequestBean@195c6e0
requestBean:example.bean.RequestBean@195c6e0
sessionBean:example.bean.SessionBean@186a259
sessionBean:example.bean.SessionBean@186a259
***********************************

通過以上結(jié)果可發(fā)現(xiàn),singleton作用域每次創(chuàng)建的實例都是一樣的。prototype作用域則每次都會創(chuàng)建新的實例。對于單個http請求,request作用域創(chuàng)建的實例是一樣的。在同一個瀏覽器中訪問屬于同一次會話,session作用域創(chuàng)建的是同一個實例對象,如果換一個瀏覽器,則會創(chuàng)建新的實例。

參考資料

關于Spring IOC (DI-依賴注入)你需要知道的一切
Spring中Bean的五個作用域

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容