Spring学习手册(5)—— bean作用域
Spring学习手册(4)—— Spring 依赖注入中介绍了Spring依赖注入的基本方式以及各种类型的参数注入的方式。本文我们详细介绍下bean的作用域。
一、Spring中bean的作用域
Spring为我们提供了7种作用域管理方式,其中singleton
、prototype
为最常使用的两个,而剩下的5个scope描述仅用于web相关的ApplicationContext
。
下表简单介绍该7种方式:
Scope | 描述 |
---|---|
singleton | (默认)当scope设置为singleton时,一个IOC容器只会对一个bean定义创建一个实例 |
prototype | 当scope设置为prototype时,每次请求bean时IOC容器会创建一个新的实例 |
request | 打个socpe设置为request时,IOC容器为每一个HTTP请求创建一个bean实例,该bean尽在该�HTTP请求内有效 |
session | 每一个HTTP请求产生一个bean实例,该实例只在该HTTP session内有效 |
globalSession | |
application | bean生命周期限定在ServletContext 生命周期内 |
websocket |
本文我们只介绍最常用的prototype
和singleton
作用域。
Tip:Spring的单例和设计模式中所提到的单例并不相同:
Spring的单例:一个IOC容器仅创建一个bean实例;
设计模式的单例:ClassLoader中只有一个class存在;
二、singleton 作用域
当将bean的scope设置为singleton
,Spring的IOC容器将仅生成和管理一个bean实例,且当你是用id或name获取bean实例时,IOC容器会返回共享的bean实例。如下图所示,不同的bean引用accountDao,IOC容器为它们返回同一个实例的引用。
![](https://img.haomeiwen.com/i871305/546809cfb8056b31.png)
由于singleton
是为scope的默认方式,因此我们有两种方式将bean的scope设置为singleton
。
<bean id="accountService" class="com.foo.DefaultAccountService"/>
<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/>
三、prototype作用域
当bean的scope设置为prototype
,Spring的IOC容器会在每次请求该bean时,都会创建一个新的实例出来。如下图所示,不同的bean定义需要注入accountDao,由于accountDao配置的作用域为prototype
,所以IOC容器为每个bean创建一个新的accountDao实例。
![](https://img.haomeiwen.com/i871305/6a928fceec340af2.png)
prototype作用域配置也很简单,只需要在配置bean定义时将scope属性配置为prototype。
<bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/>
四、例子
为测试以上性质,我们创建PrototypeBean
、SingletonBean
类,类内部有计数器,每当一个新的实例被创建时,内部计数器加1�。代码如下:
PrototypeBean:
public class PrototypeBean {
private static int num = 0;
private int count;
public PrototypeBean() {
num++;
count = num;
}
public int getCount() {
return count;
}
}
SingletonBean:
public class SingletonBean {
private static int num =0;
private int count;
public SingletonBean() {
num++;
count = num;
}
public int getCount() {
return count;
}
}
为了使用该类,我们创建UsePrototypeBeanExample类系列和UseSingletonBeanExample类系列,该系列类实现方式相同,只是类名不同,这里我们列出一个例子:
public class UsePrototypeBeanExample1 {
private PrototypeBean prototypeBean;
public void setPrototypeBean(PrototypeBean prototypeBean) {
this.prototypeBean = prototypeBean;
}
public PrototypeBean getPrototypeBean() {
return prototypeBean;
}
}
以上实验代码准备好后,我们将配置组装我们的bean信息:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<!-- 定义prototype bean -->
<bean id="prototypeBean" class="com.liangwei.learnspring.PrototypeBean" scope="prototype"/>
<!-- 定义singleton bean-->
<bean id="singletonBean" class="com.liangwei.learnspring.SingletonBean" scope="singleton"/>
<!-- 依赖 prototype bean 属性注入-->
<bean id="prototypeExample1" class="com.liangwei.learnspring.UsePrototypeBeanExample1">
<property name="prototypeBean" ref="prototypeBean"/>
</bean>
<bean id="prototypeExample2" class="com.liangwei.learnspring.UsePrototypeBeanExample2">
<property name="prototypeBean" ref="prototypeBean"/>
</bean>
<bean id="prototypeExample3" class="com.liangwei.learnspring.UsePrototypeBeanExample3">
<property name="prototypeBean" ref="prototypeBean"/>
</bean>
<!--依赖singleton bean 属性注入-->
<bean id="singletonExample1" class="com.liangwei.learnspring.UseSingletonBeanExample1">
<property name="singletonBean" ref="singletonBean"/>
</bean>
<bean id="singletonExample2" class="com.liangwei.learnspring.UseSingletonBeanExample2">
<property name="singletonBean" ref="singletonBean"/>
</bean>
<bean id="singletonExample3" class="com.liangwei.learnspring.UseSingletonBeanExample3">
<property name="singletonBean" ref="singletonBean"/>
</bean>
</beans>
测试代码:
public class Application {
public static void main(String[] args){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
System.out.println("Prototype bean test:");
UsePrototypeBeanExample1 usePrototypeBeanExample1 = applicationContext.getBean("prototypeExample1",UsePrototypeBeanExample1.class);
System.out.println(usePrototypeBeanExample1.getPrototypeBean().getCount());
UsePrototypeBeanExample2 usePrototypeBeanExample2 = applicationContext.getBean("prototypeExample2",UsePrototypeBeanExample2.class);
System.out.println(usePrototypeBeanExample2.getPrototypeBean().getCount());
UsePrototypeBeanExample3 usePrototypeBeanExample3 = applicationContext.getBean("prototypeExample3",UsePrototypeBeanExample3.class);
System.out.println(usePrototypeBeanExample3.getPrototypeBean().getCount());
System.out.println(applicationContext.getBean("prototypeBean",PrototypeBean.class).getCount());
System.out.println("\n singleton bean test:");
UseSingletonBeanExample1 useSingletonBeanExample1 = applicationContext.getBean("singletonExample1",UseSingletonBeanExample1.class);
System.out.println(useSingletonBeanExample1.getSingletonBean().getCount());
UseSingletonBeanExample2 useSingletonBeanExample2 = applicationContext.getBean("singletonExample2",UseSingletonBeanExample2.class);
System.out.println(useSingletonBeanExample2.getSingletonBean().getCount());
UseSingletonBeanExample3 useSingletonBeanExample3 = applicationContext.getBean("singletonExample3",UseSingletonBeanExample3.class);
System.out.println(useSingletonBeanExample3.getSingletonBean().getCount());
System.out.println(applicationContext.getBean("singletonBean",SingletonBean.class).getCount());
}
}
运行代码我们会发现输出结果如下:
Prototype bean test:
1
2
3
4
singleton bean test:
1
1
1
1
通过测试实验代码我们验证了如下结论:
prototype作用域:
- 需要获取prototype作用域的bean时会创建一个全新的bean实例;
- 无论使用属性注入还是使用
getBean
的方式都会获得一个新的bean实例
singleton作用域: - 需要获取singleton作用域的bean时会共享同一个bean实例;
- 无论使用属性注入还是使用
getBean
的方式获得的都为共享的bean实例。
测试代码下载地址