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