前言
Bean的作用域是指spring容器從創(chuàng)建Bean到銷毀的整個過程。Spring容器創(chuàng)建的bean默認是singleton單例模式,即每個Bean的實例只創(chuàng)建一次。Spring可以為Bean指定以下五種作用域。
- singleton,每個Bean的實例只創(chuàng)建一次。
- prototype,每次獲取Bean實例時都會新創(chuàng)建一個實例對象。
- request,對于每次HTTP請求,Spring容器都會創(chuàng)建一個Bean實例,整個請求過程使用相同的bean實例。不同的請求創(chuàng)建新的實例。
- session,對于HTTP Session,每次會創(chuàng)建一個Session作用域的Bean。
- globalsession,每個全局的HTTP Session,使用session定義的Bean都將產(chǎn)生一個新實例。
舉例說明
新建類,并指定作用域。
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,連續(xù)運行兩次,觀察輸出結(jié)果如下。
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
***********************************
通過以上結(jié)果可發(fā)現(xiàn),singleton作用域每次創(chuàng)建的實例都是一樣的。prototype作用域則每次都會創(chuàng)建新的實例。對于單個http請求,request作用域創(chuàng)建的實例是一樣的。在同一個瀏覽器中訪問屬于同一次會話,session作用域創(chuàng)建的是同一個實例對象,如果換一個瀏覽器,則會創(chuàng)建新的實例。