04-Spring中Bean的作用域

Bean的作用域

scope的属性值

属性值 作用
singleton 默认单例
prototype 原型每调用一次getBean()方法则获取一个新的Bean对象 , 每次注入的时候都是新对象
request 一个请求对应一个Bean仅限于在WEB应用中使用 , 需要引入web的框架如SpringMvc
(global) session 一个会话对应一个Bean仅限于在WEB应用中使用 , 需要引入web的框架如SpringMvc
global session portlet应用中专用的, portlet和servlet都是规范, servlet运行在servlet容器中如Tomcat, portlet运行在portlet容器中
application 一个应用对应一个Bean仅限于在WEB应用中使用
websocket 一个websocket生命周期对应一个Bean仅限于在WEB应用中使用
自定义scope 很少使用

scope属性值singleton(单例)

Spring的IoC容器中默认情况下Bean对象是单例的

  • Bean对象的创建是在初始化Spring上下文的时候就完成的 , 执行getBean()方法的时候Bean对象已经创建好了 , 每次获取操作的都是那个单例的Bean对象
java 复制代码
public class SpringBean {
    public SpringBean() {
        System.out.println("SpringBean的无参数构造方法执行");
    }
}
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对象是单例的,scope="singleton"可以省略-->
    <bean id="sb" class="com.powernode.spring6.beans.SpringBean" />
</beans>
java 复制代码
@Test
public void testScope(){
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml");
    SpringBean sb1 = applicationContext.getBean("sb", SpringBean.class);
    //com.powernode.spring6.beans.SpringBean@5b239d7d
    System.out.println(sb1);

    //com.powernode.spring6.beans.SpringBean@5b239d7d
    SpringBean sb2 = applicationContext.getBean("sb", SpringBean.class);
    System.out.println(sb2);
}

scope属性值prototype(多例)

如果想让Spring的Bean对象以多例的形式存在,可以在bean标签中指定scope属性的值为prototype

  • 初始化Spring上下文时不会创建这些prototype的Bean对象 , 只有每一次执行getBean()方法的时候才会创建,调用几次创建几次 , 每次获取的都是新的对象
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 复制代码
@Test
public void testScope(){
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-scope.xml");
    //com.powernode.spring6.beans.SpringBean@29445a7
    SpringBean sb1 = applicationContext.getBean("sb", SpringBean.class);
    System.out.println(sb1);
	//com.powernode.spring6.beans.SpringBean@67d48005
    SpringBean sb2 = applicationContext.getBean("sb", SpringBean.class);
    System.out.println(sb2);
}

自定义线程级别Scope(了解)

第一步: 自定义Scope实现Scope接口,实现在同一个线程中获取的Bean都是同一个, 跨线程获取的是不同的Bean对象

  • Spring内置了实现Scope接口的线程范围的类org.springframework.context.support.SimpleThreadScope

第二步: 使用CustomScopeConfigurer内置类将自定义的Scope注册到Spring容器中

xml 复制代码
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
  <!--设置scopes属性,这个属性是个map集合表示可以指定多个线程范围-->
  <property name="scopes">
    <map>
      <!--自定义线程名-->
      <entry key="myThread">
        <!--这个Scope接口的实现类使用的是Spring内置的,也可以自己定义-->
        <bean class="org.springframework.context.support.SimpleThreadScope"/>
      </entry>
    </map>
  </property>
</bean>

第三步: 使用自定义线程的Scope

xml 复制代码
<bean id="sb" class="com.powernode.spring6.beans.SpringBean" scope="myThread" />
java 复制代码
@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);
    //com.powernode.spring6.beans.SpringBean@5b236d7d
    System.out.println(sb1);
    //com.powernode.spring6.beans.SpringBean@5b236d7d
    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);
            //com.powernode.spring6.beans.SpringBean@29445a6
            System.out.println(a);
            //com.powernode.spring6.beans.SpringBean@29445a6
            System.out.println(b);
        }
    }).start();
}
相关推荐
Configure-Handler8 小时前
buildroot System configuration
java·服务器·数据库
:Concerto8 小时前
JavaSE 注解
java·开发语言·sprint
电商API_180079052479 小时前
第三方淘宝商品详情 API 全维度调用指南:从技术对接到生产落地
java·大数据·前端·数据库·人工智能·网络爬虫
一点程序9 小时前
基于SpringBoot的选课调查系统
java·spring boot·后端·选课调查系统
C雨后彩虹9 小时前
计算疫情扩散时间
java·数据结构·算法·华为·面试
2601_949809599 小时前
flutter_for_openharmony家庭相册app实战+我的Tab实现
java·javascript·flutter
vx_BS8133010 小时前
【直接可用源码免费送】计算机毕业设计精选项目03574基于Python的网上商城管理系统设计与实现:Java/PHP/Python/C#小程序、单片机、成品+文档源码支持定制
java·python·课程设计
2601_9498683610 小时前
Flutter for OpenHarmony 电子合同签署App实战 - 已签合同实现
java·开发语言·flutter
达文汐10 小时前
【困难】力扣算法题解析LeetCode332:重新安排行程
java·数据结构·经验分享·算法·leetcode·力扣
培风图南以星河揽胜10 小时前
Java版LeetCode热题100之零钱兑换:动态规划经典问题深度解析
java·leetcode·动态规划