终于搞懂Spring中Scope为Request和Session的Bean了


之前只是很模糊的知道其意思,在request scope中,每个request创建一个新的bean,在session scope中,同一session中的bean都是一样的

但是不知道怎么用代码去验证它

今天可找到验证它的代码了

首先定义一个简单的类

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();
    }
}

最后定义个控制类

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"
}


原文链接:https://www.cnblogs.com/eaglelihh/p/13340749.html