在默认情况下,Spring应用上下文中所有bean都是作为以单例(singleton)的形式创建的。也就是说,不管给定的一个bean被注入到其他bean多少次,每次所注入的都是同一个实例。
在大多数情况下,单例bean是很理想的方案。初始化和垃圾回收对象实例所带来的成本只留给一些小规模任务,在这些任务中,让对象保持无状态并且在应用中反复重用这些对象可能并不合理。
有时候,可能会发现,你所使用的类是易变的(mutable),它们会保持一些状态,因此重用是不安全的。在这种情况下,将class声明为单例的bean就不是什么好主意了,因为对象会被污染,稍后重用的时候会出现意想不到的问题。
Spring定义了多种作用域,可以基于这些作用域创建bean,包括:
- 单例(Singleton):在整个应用中,只创建bean的一个实例。
- 原型(Prototype):每次注入或者通过Spring应用上下文获取的时候,都会创建一个新的bean实例。
- 会话(Session):在Web应用中,为每个会话创建一个bean实例。
- 请求(Rquest):在Web应用中,为每个请求创建一个bean实例。
单例是默认的作用域,但是正如之前所述,对于易变的类型,这并不合适。如果选择其他的作用域,要使用@Scope
注解,它可以与@Component
或@Bean
一起使用。
例如,如果你使用组件扫描来发现和声明bean,那么你可以在bean的类上使用@Scope
注解,将其声明为原型bean
这里,使用ConfigurableBeanFactory
类的SCOPE_PROTOTYPE
常量设置了原型作用域。你当然也可以使用@Scope("prototype")
,但是使用SCOPE_PROTOTYPE
常量更加安全并且不易出错。
如果你想在Java配置中将Notepad
声明为原型bean,那么可以组合使用@Scope
和@Bean
来指定所需的作用域:
同样,如果你使用XML来配置bean的话,可以使用<bean>
元素的scope属性来设置作用域:
不管你使用哪种方式来声明原型作用域,每次注入或从Spring应用上下文中检索该bean的时候,都会创建新的实例。这样所导致的结果就是每次操作都能得到自己的Notepad
实例
以下是小小的例子测试下Spring bean的作用域:
目录:
JavaConfig 和 MvcConfig 分别为 spring 和springMVC的配置文件,其余几个Bean结尾的文件为 四种作用域的类。
测试类在TestServlet中。
TestServlet:
package beanscope;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@Scope("prototype")
public class TestServlet {
@Autowired
private RequestBean RequestObj;
@Autowired
private SessionBean SessionObj;
@Autowired
private ProtoBean PrototypeObj;
@Autowired
private SingleBean SingletonObj;
@Autowired
private ApplicationContext applicationContext;
@RequestMapping("/")
@ResponseBody
public String index() {
print();
return "Welcome";
}
public RequestBean getRequestObj() {
return RequestObj;
}
public void setRequestObj(RequestBean requestObj) {
RequestObj = requestObj;
}
public SessionBean getSessionObj() {
return SessionObj;
}
public void setSessionObj(SessionBean sessionObj) {
SessionObj = sessionObj;
}
public ProtoBean getPrototypeObj() {
return PrototypeObj;
}
public void setPrototypeObj(ProtoBean prototypeObj) {
PrototypeObj = prototypeObj;
}
public SingleBean getSingletonObj() {
return SingletonObj;
}
public void setSingletonObj(SingleBean singletonObj) {
SingletonObj = singletonObj;
}
public void setApplicationContext(ApplicationContext act)
throws BeansException {
this.applicationContext=act;
}
public void print() {
int count =1;
System.out.println(count+" time singleton is :" + getSingletonObj());
System.out.println(count+"second time prototype is :" + getPrototypeObj());
System.out.println(count+"first time request is :" + getRequestObj());
System.out.println(count+"second time session is :" + getSessionObj());
count++;
}
}
singleBean:
package beanscope;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("singleton")
public class SingleBean {
}
protoBean:
package beanscope;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("prototype")
public class ProtoBean {
}
reuqestBean:
package beanscope;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("request")
public class RequestBean {
}
sessionBean:
package beanscope;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("session")
public class SessionBean {
}
web.xml 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or space-delimited
fully-qualified @Configuration classes. Fully-qualified packages may also be
specified for component-scanning -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>beanscope.JavaConfig</param-value>
</context-param>
<!-- Bootstrap the root application context as usual using ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<!-- Again, config locations must consist of one or more comma- or space-delimited
and fully-qualified @Configuration classes 加载springMVC初始化配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>beanscope.MvcConfig</param-value>
</init-param>
</servlet>
<!-- map all requests for /app/* to the dispatcher servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
启动tomcat,在浏览器中输入:http://localhost:8080/springPortal/
查看控制台输出:
1 time singleton is :beanscope.SingleBean@2581af84
1 time prototype is :beanscope.ProtoBean@6ca02cea
1 time request is :beanscope.RequestBean@7e4ee5d
1 time session is :beanscope.SessionBean@8d8c96
然后再选中浏览器地址栏 按回车再次发送请求:
1 time singleton is :beanscope.SingleBean@2581af84
1 time prototype is :beanscope.ProtoBean@59817e19
1 time request is :beanscope.RequestBean@447dc937
1 time session is :beanscope.SessionBean@8d8c96
可以看到单例模式始终都是 访问同一个对象,不同请求也是生成不同的对象, 因为浏览器没关session一直存在同一个session域
也一直是同一个对象