終於搞懂Spring中Scope爲Request和Session的Bean了

以前只是很模糊的知道其意思,在request scope中,每一個request建立一個新的bean,在session scope中,同一session中的bean都是同樣的html

可是不知道怎麼用代碼去驗證它java

今天可找到驗證它的代碼了web

首先定義一個簡單的類spring

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class HelloMessageGenerator {
    private String message;

    @Override
    public String toString() {
        return getClass().getSimpleName() + "@" + Integer.toHexString(hashCode());
    }
}

而後定義一個配置類瀏覽器

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.annotation.RequestScope;
import org.springframework.web.context.annotation.SessionScope;

@Configuration
public class ScopesConfig {
    @Bean
    @RequestScope
    public HelloMessageGenerator requestScopedBean() {
        return new HelloMessageGenerator();
    }

    @Bean
    @SessionScope
    public HelloMessageGenerator sessionScopedBean() {
        return new HelloMessageGenerator();
    }
}

最後定義個控制類session

import com.google.common.collect.Maps;
import java.util.Map;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/scopes")
@Slf4j
public class ScopesController {

    @Resource(name = "requestScopedBean")
    private HelloMessageGenerator requestScopedBean;

    @Resource(name = "sessionScopedBean")
    private HelloMessageGenerator sessionScopedBean;

    @GetMapping("/request")
    public Map<String, String> getRequestScopeMessage() {
        Map<String, String> result = Maps.newLinkedHashMap();
        result.put("message", requestScopedBean.getMessage());
        result.put("requestScopedBean", requestScopedBean.toString());
        requestScopedBean.setMessage("Good morning!");
        return result;
    }

    @GetMapping("/session")
    public Map<String, String> getSessionScopeMessage() {
        log.info("getSessionScopeMessage(), sessionScopedBean:{}", sessionScopedBean);
        Map<String, String> result = Maps.newLinkedHashMap();
        result.put("message", sessionScopedBean.getMessage());
        result.put("sessionScopedBean", sessionScopedBean.toString());
        sessionScopedBean.setMessage("Good morning!");
        return result;
    }
}
結果說明:
1. 對於請求:/scopes/request(request scope 請求地址),網頁上的結果是:
對於任何一次請求,message爲null,且requestScopedBean都不一致,因此是每一個request都會從新建立一個bean
第一次請求:
{
      message: null,
      requestScopedBean: "HelloMessageGenerator@3ef0cae5"
}
以後的請求:
{
      message: null,
      requestScopedBean: "HelloMessageGenerator@c84a4f4"
}

2. 對於請求:/scopes/session(session scope 請求地址),網頁上的結果始終是:
第一次請求,message爲null,以後的每次請求message被置爲Good morning!,而且sessionScopedBean固定爲一個值,因此同一session下都使用着同一個bean
當從新換一個瀏覽器請求時,又會重複以上的操做
第一次請求:
{
      message: null,
      sessionScopedBean: "HelloMessageGenerator@f9f38d5"
}
以後的請求:
{
      message: "Good morning!",
      sessionScopedBean: "HelloMessageGenerator@f9f38d5"
}

spring-bean-scopes - baeldung
spring 組件@Scope(request,session)示例 - cnblogsapp

相關文章
相關標籤/搜索