Spring Bean作用域测试

作者:Tomandy
链接:https://www.jianshu.com/p/cdf93e2de739
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

前言

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作用域创建的是同一个实例对象,如果换一个浏览器,则会创建新的实例。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值