Spring6-5.Bean 的作用域

88 阅读2分钟

Bean 的作用域

singleton(默认值)

默认情况下,Spring 的 Ioc 容器创建的 Bean 对象是单例的,实例对象是在初始化 Spring 上下文的时候就完成的。

prototype

如果想让 Spring 的 Bean 对象以多例的形式存在,可以在 bean 标签中指定 scope 属性的值为:prototype,这样 Spring会在每一次执行 getBean 方法的时候创建 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.xsd">
​
    <bean id="sb" class="com.powernode.spring6.beans.SpringBean" scope="prototype" /></beans>
@Test
public void testScope(){
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml");
        SpringBean springBean = applicationContext.getBean("springBean", SpringBean.class);
        System.out.println(springBean);
​
        SpringBean springBean1 = applicationContext.getBean("springBean", SpringBean.class);
        System.out.println(springBean1);
​
        SpringBean springBean2 = applicationContext.getBean("springBean", SpringBean.class);
        System.out.println(springBean2);
}

spring-scope-customerscope.png

spring-scope-prototype.png 当使用 prototype 属性的时候,在初始化 Spring 上下文的事哦呼,并没有创建 bean 对象。

其他 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 都是同一个。跨线程则是不同的对象:(以下内容作为了解)

  1. 第一步:自定义 Scope 类,实现 Scope 接口。

    • spring内置了线程范围的类:org.springframework.context.support.SimpleThreadScope,可以直接用。
  2. 第二步:将自定义的 Scope 注册到 Spring 容易中。

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
  <property name="scopes">
    <map>
      <entry key="myThread">
        <bean class="org.springframework.context.support.SimpleThreadScope"/>
      </entry>
    </map>
  </property>
</bean>
  1. 第三步:使用 Scope。
<bean id="sb" class="com.powernode.spring6.beans.SpringBean" scope="myThread" />

编写测试程序:

package com.powernode.spring6.test;
​
import com.powernode.spring6.bean.SpringBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
​
public class SpringBeanScopeTest {
​
    @Test
    public void testThreadScope() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml");
        SpringBean springBean = applicationContext.getBean("springBean", SpringBean.class);
        System.out.println(springBean);
​
        SpringBean springBean1 = applicationContext.getBean("springBean", SpringBean.class);
        System.out.println(springBean1);
​
        // 启动线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                SpringBean springBean = applicationContext.getBean("springBean", SpringBean.class);
                System.out.println(springBean);
                SpringBean springBean1 = applicationContext.getBean("springBean", SpringBean.class);
                System.out.println(springBean1);
            }
        }).start();
    }
}

指定结果:

spring-scope-customerscope.png