Skip to content

05 Bean的作用域

5.1 singleton

默认情况下,Spring 的 IoC 容器创建的 Bean 对象是单例的。

来测试一下: ```java title:SpringBean package com.powernode.spring6.beans;

/** * @author 动力节点 * @version 1.0 * @className SpringBean * @since 1.0 */ public class SpringBean { }

```xml title:spring-scope.xml
<?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.xsd">

    <bean id="sb" class="com.powernode.spring6.beans.SpringBean"/>
</beans>

```java title:testScope @Test public void testScope(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml");

SpringBean sb1 = applicationContext.getBean("sb", SpringBean.class);
System.out.println(sb1);

SpringBean sb2 = applicationContext.getBean("sb", SpringBean.class);
System.out.println(sb2);

} 执行结果: ![[05.01-1.png]] 通过测试得知:Spring 的 IoC 容器中,默认情况下,Bean 对象是单例的。 这个对象在什么时候创建的呢?可以为 SpringBean 提供一个无参数构造方法,测试一下,如下:java title:SpringBean package com.powernode.spring6.beans; /** * @author 动力节点 * @version 1.0 * @className SpringBean * @since 1.0 */ public class SpringBean { public SpringBean() { System.out.println("SpringBean的无参数构造方法执行。"); } } ```

将测试程序中 getBean () 所在行代码注释掉:

```java title:testScope @Test public void testScope(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml"); }

执行测试程序:
![[05.01-2.png]]
通过测试得知,**默认情况下,Bean 对象的创建是在初始化 Spring 上下文的时候就完成的**。


---
## 5.2 prototype

如果想让 Spring 的 Bean 对象以多例的形式存在,可以在 bean 标签中指定 scope 属性的值为:prototype,这样 Spring 会在每一次执行 getBean () 方法的时候创建 Bean 对象,调用几次则创建几次。

```xml title:spring-scope.xml
<?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.xsd">

    <bean id="sb" class="com.powernode.spring6.beans.SpringBean" scope="prototype"/>
</beans>

```java title:testScope @Test public void testScope(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml");

SpringBean sb1 = applicationContext.getBean("sb", SpringBean.class);
System.out.println(sb1);

SpringBean sb2 = applicationContext.getBean("sb", SpringBean.class);
System.out.println(sb2);

} 执行结果: ![[05.02-1.png]] 我们可以把测试代码中的 getBean () 方法所在行代码注释掉:java title:testScope @Test public void testScope(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml"); } ```

执行结果: ![[05.02-2.png]]

可以看到这一次在初始化 Spring 上下文的时候,并没有创建 Bean 对象。

那你可能会问:scope 如果没有配置,它的默认值是什么呢?默认值是 singleton,单例的。

```xml title:spring-scope.xml

<bean id="sb" class="com.powernode.spring6.beans.SpringBean"/>

java title:testScope @Test public void testScope(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml"); SpringBean sb1 = applicationContext.getBean("sb", SpringBean.class); System.out.println(sb1); SpringBean sb2 = applicationContext.getBean("sb", SpringBean.class); System.out.println(sb2); } ```

执行结果: ![[05.02-3.png]] 通过测试得知,没有指定 scope 属性时,默认是 singleton 单例的。


5.3 其它 scope

scope 属性的值不止两个,它一共包括 8 个选项: - singleton:默认的,单例。 - prototype:原型。每调用一次 getBean () 方法则获取一个新的 Bean 对象。或每次注入的时候都是新对象。 - request:一个请求对应一个 Bean。仅限于在 WEB 应用中使用。 - session:一个会话对应一个 Bean。仅限于在 WEB 应用中使用。 - global session:portlet 应用中专用的。如果在 Servlet 的 WEB 应用中使用 global session 的话,和 session 一个效果。(portlet 和 servlet 都是规范。servlet 运行在 servlet 容器中,例如 Tomcat。portlet 运行在 portlet 容器中。) - application:一个应用对应一个 Bean。仅限于在 WEB 应用中使用。 - websocket:一个 websocket 生命周期对应一个 Bean。仅限于在 WEB 应用中使用。 - 自定义 scope:很少使用。

接下来咱们自定义一个 Scope,线程级别的 Scope,在同一个线程中,获取的 Bean 都是同一个。跨线程则是不同的对象:(以下内容作为了解) - 第一步:自定义 Scope。(实现 Scope 接口) - spring 内置了线程范围的类:org.springframework.context.support. ``SimpleThreadScope,可以直接用。 - 第二步:将自定义的 Scope 注册到 Spring 容器中。 ```XML title:spring-scope.xml

- 第三步:使用 Scope。
```XML title:spring-scope.xml
<bean id="sb" class="com.powernode.spring6.beans.SpringBean" scope="myThread" />

编写测试程序:

@Test
public void testCustomScope(){
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml");
    SpringBean sb1 = applicationContext.getBean("sb", SpringBean.class);
    SpringBean sb2 = applicationContext.getBean("sb", SpringBean.class);
    System.out.println(sb1);
    System.out.println(sb2);
    // 启动线程
    new Thread(new Runnable() {
        @Override
        public void run() {
            SpringBean a = applicationContext.getBean("sb", SpringBean.class);
            SpringBean b = applicationContext.getBean("sb", SpringBean.class);
            System.out.println(a);
            System.out.println(b);
        }
    }).start();
}

执行结果: ![[Java/Java_Spring/attachments/05 Bean的作用域/05.03-1.png]]