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

前言

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

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

舉例說明

新建類,并指定作用域。

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,連續運行兩次,觀察輸出結果如下。

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
***********************************

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

參考資料

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

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容