spring bean的作用域scope之间有什么区别demo

本文主要通过demo来测试配置不同作用域,查看请求的实例有何不一样,根据是打印hashcode值,(同一个实例的hashcode值是一样的)

demo转自: https://blog.csdn.net/panhaigang123/article/details/79452064

作用域之前的区别请查看: https://blog.csdn.net/qiaziliping/article/details/77209951

有状态bean和无状态bean
https://www.iteye.com/topic/960532

// 用@Component 组件也可以
@Service
@Scope(value = "singleton")
public class SingletonService {}

@Service
@Scope(value = "prototype")
public class PrototypeService {}

@Service
@Scope(value = "request")
public class RequestService {}

@Service
@Scope(value = "session")
public class SessionService {}


使用默认单例的控制类测试,注入到controller,由于ScopeController 是单例的,因此必须通过实现ApplicationContextAware接口,直接从容器中取出对象。

@RequestMapping(value = "/scope")
@RestController
public class ScopeController implements ApplicationContextAware {


    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private int count;

    private ApplicationContext applicationContext;

    private SingletonService singletonService;
    private PrototypeService prototypeService;
    private RequestService requestService;
    private SessionService sessionService;


    @RequestMapping(value = "/one")
    public String one() {

        count ++;
        logger.info("count第一次-->:"+count);

        logger.info("singletom第一次-->:"+getSingletonService().hashCode());
        logger.info("prototype第一次-->:"+getPrototypeService().hashCode());
        logger.info("request第一次-->:"  +getRequestService().hashCode());
        logger.info("session第一次-->:"  +getSessionService().hashCode());

        Map<String,Object> map = new HashMap<String,Object>();
        map.put(count+"-singletonService.hashCode()",getSingletonService().hashCode());
        map.put(count+"-prototypeService.hashCode()",getPrototypeService().hashCode());
        map.put(count+"-requestService.hashCode()",  getRequestService().hashCode());
        map.put(count+"-sessionService.hashCode()",getSessionService().hashCode());

        return map.toString();
    }



    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public SingletonService getSingletonService() {
        return applicationContext.getBean(SingletonService.class);
    }
    public void setSingletonService(SingletonService singletonService) {
        this.singletonService = singletonService;
    }

    public PrototypeService getPrototypeService() {
        return applicationContext.getBean(PrototypeService.class);
    }
    public void setPrototypeService(PrototypeService prototypeService) {
        this.prototypeService = prototypeService;
    }
    public RequestService getRequestService() {
        return applicationContext.getBean(RequestService.class);
    }

    public void setRequestService(RequestService requestService) {
        this.requestService = requestService;
    }

    public SessionService getSessionService() {
        return applicationContext.getBean(SessionService.class);
    }

    public void setSessionService(SessionService sessionService) {
        this.sessionService = sessionService;
    }
}

  • 1、对比第一次和第二次查看的结果可以看出singleton每次请求共享一个实例,request每次的hashcode都不一样,prototype没调用一次的hashcode都不一样;
  • 2、对比第二次和第三次的结果:session作用域对应每个session的实例是不一样
  • 3、对比count值你会发现spring的bean默认是单例的singleton

google浏览器第一次请求结果>:
{count>:1-sessionService.hashCode()=743832415, count>:1-prototypeService.hashCode()=197374677, count>:1-requestService.hashCode()=111483993, count>:1-singletonService.hashCode()=333211209}

google浏览器第二次请求结果>:
{count>:2-prototypeService.hashCode()=1038644898, count>:2-requestService.hashCode()=970450240, count>:2-singletonService.hashCode()=333211209, count>:2-sessionService.hashCode()=743832415}

IE浏览器第一次请求结果>:
{count>:3-prototypeService.hashCode()=867891049, count>:3-singletonService.hashCode()=333211209, count>:3-requestService.hashCode()=1921514834, count>:3-sessionService.hashCode()=1991796698}

@Scope(value = "prototype")
@RequestMapping(value = "/scope/pp")
@RestController
public class ScopePrototypeController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private int count;

    @Autowired
    private SingletonService singletonService;
    @Autowired
    private PrototypeService prototypeService;
    @Autowired
    private RequestService requestService;
    @Autowired
    private SessionService sessionService;

    @RequestMapping(value = "/one")
    public String one() {

        count ++;
        logger.info("count第一次-->:"+count);

        logger.info("singletom第一次-->:"+singletonService.hashCode());
        logger.info("prototype第一次-->:"+prototypeService.hashCode());
        logger.info("request第一次-->:"  +requestService.hashCode());
        logger.info("session第一次-->:"  +sessionService.hashCode());

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("count>:"+count+"-singletonService.hashCode()",singletonService.hashCode());
        map.put("count>:"+count+"-prototypeService.hashCode()",prototypeService.hashCode());
        map.put("count>:"+count+"-requestService.hashCode()",  requestService.hashCode());
        map.put("count>:"+count+"-sessionService.hashCode()",sessionService.hashCode());
        return map.toString();
    }
}

  • 对比一下你会发现count值一直为1,controller的bean是prototype
    google浏览器第一次请求结果>:
    {count>:1-sessionService.hashCode()=743832415, count>:1-prototypeService.hashCode()=124664069, count>:1-requestService.hashCode()=2136094965, count>:1-singletonService.hashCode()=333211209}

google浏览器第二次请求结果>:
{count>:1-sessionService.hashCode()=743832415, count>:1-prototypeService.hashCode()=1477368302, count>:1-requestService.hashCode()=2003931014, count>:1-singletonService.hashCode()=333211209}

IE浏览器第一次请求结果>:
{count>:1-sessionService.hashCode()=1991796698, count>:1-prototypeService.hashCode()=1067907335, count>:1-requestService.hashCode()=828440923, count>:1-singletonService.hashCode()=333211209}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值