【Spring】Bean 的作用域

一、singleton

默认情况下,Spring 的 IoC 容器创建的 Bean 对象是单例的(单例模式)

默认情况下,Bean 对象的创建是在初始化 Spring 上下文的时候就完成的

package org.qiu.spring.bean;

/**
 * @author 秋玄
 * @version 1.0
 * @email qiu_2022@aliyun.com
 * @project Spring
 * @package org.qiu.spring.bean
 * @date 2022-11-09-11:27
 * @since 1.0
 */
public class SpringBean {
}
<?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="springBean" class="org.qiu.spring.bean.SpringBean"/>

</beans>
@Test
public void testBeanScope(){
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml");
    SpringBean springBean1 = applicationContext.getBean("springBean", SpringBean.class);
    System.out.println(springBean1);

    SpringBean springBean2 = applicationContext.getBean("springBean", SpringBean.class);
    System.out.println(springBean2);

    SpringBean springBean3 = applicationContext.getBean("springBean", SpringBean.class);
    System.out.println(springBean3);
}

运行结果:  

 

二、prototype

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

<bean id="springBean" class="org.qiu.spring.bean.SpringBean" scope="prototype"/>
public class SpringBean {
    public SpringBean(){
        System.out.println("SpringBean 无参数构造方法执行了......");
    }
}

运行结果:  

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

没有指定 scope 属性时,默认是 singleton 单例的

 

三、其他 

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 为 singleton 时,多个线程共享一个 Bean

<bean id="springBean" class="org.qiu.spring.bean.SpringBean" scope="singleton"/>
@Test
public void testThreadScope(){
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml");
    SpringBean springBean = applicationContext.getBean("springBean", SpringBean.class);
    System.out.println(springBean);

    // 启动一个线程
    new Thread(new Runnable() {
        @Override
        public void run() {
            SpringBean springBean1 = applicationContext.getBean("springBean", SpringBean.class);
            System.out.println(springBean1);
        }
    }).start();
}

运行结果:  

 

  • 第一步:自定义 Scope(实现 Scope 接口)

Spring 内置了线程范围的类:org.springframework.context.support.SimpleThreadScope,可以直接用

  • 第二步:将自定义的 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>
  • 使用Scope

<bean id="springBean" class="org.qiu.spring.bean.SpringBean" scope="myThread"/>
  • 测试

@Test
public void testThreadScope(){
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml");
    SpringBean springBean = applicationContext.getBean("springBean", SpringBean.class);
    System.out.println(springBean);

    // 启动一个线程
    new Thread(new Runnable() {
        @Override
        public void run() {
            SpringBean springBean1 = applicationContext.getBean("springBean", SpringBean.class);
            System.out.println(springBean1);
        }
    }).start();
}

运行结果:  

 

一  叶  知  秋,奥  妙  玄  心

  • 20
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qx_java_1024

祝老板生意兴隆,财源广进!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值