SPRING TUTORIAL SPRING TUTORIAL
综合篇
核心篇
数据篇
Web篇
IO篇
集成篇
其他
GitHub (opens new window)
综合篇
核心篇
数据篇
Web篇
IO篇
集成篇
其他
GitHub (opens new window)
  • 框架

    • Spring

      • Spring综合

      • Spring核心

        • Spring Bean
        • Spring IoC
        • Spring 依赖查找
        • Spring 依赖注入
        • Spring IoC 依赖来源
        • Spring Bean 作用域
          • Spring Bean 作用域
          • "singleton" Bean 作用域
          • "prototype" Bean 作用域
          • "request" Bean 作用域
          • "session" Bean 作用域
          • "application" Bean 作用域
          • 自定义 Bean 作用域
          • 问题
          • 参考资料
        • Spring Bean 生命周期
        • Spring 配置元数据
        • Spring 应用上下文生命周期
        • Spring AOP
        • Spring 资源管理
        • Spring 校验
        • Spring 数据绑定
        • Spring 类型转换
        • Spring EL 表达式
        • Spring 事件
        • Spring 国际化
        • Spring 泛型处理
        • Spring 注解
        • Spring Environment 抽象
        • SpringBoot 之快速入门
        • SpringBoot 之属性加载详解
        • SpringBoot 之 Profile
      • Spring数据

      • SpringWeb

      • SpringIO

      • Spring集成

      • Spring安全

      • Spring其他

  • Java
  • 框架
  • Spring
  • Spring核心
dunwu
2022-12-21
目录

Spring Bean 作用域

# Spring Bean 作用域

# Spring Bean 作用域

来源 说明
singleton 默认 Spring Bean 作用域,一个 BeanFactory 有且仅有一个实例
prototype 原型作用域,每次依赖查找和依赖注入生成新 Bean 对象
request 将 Spring Bean 存储在 ServletRequest 上下文中
session 将 Spring Bean 存储在 HttpSession 中
application 将 Spring Bean 存储在 ServletContext 中

# "singleton" Bean 作用域

# "prototype" Bean 作用域

Spring 容器没有办法管理 prototype Bean 的完整生命周期,也没有办法记录实例的存在。销毁回调方法将不会执行,可以利用 BeanPostProcessor 进行清扫工作。

# "request" Bean 作用域

  • 配置
    • XML - <bean class="..." scope = “request" />
    • Java 注解 - @RequestScope 或 @Scope(WebApplicationContext.SCOPE_REQUEST)
  • 实现
    • API - RequestScope

# "session" Bean 作用域

  • 配置
    • XML - <bean class="..." scope = “session" />
    • Java 注解 - @SessionScope 或 @Scope(WebApplicationContext.SCOPE_SESSION)
  • 实现
    • API - SessionScope

# "application" Bean 作用域

  • 配置
    • XML - <bean class="..." scope = “application" />
    • Java 注解 - @ApplicationScope 或 @Scope(WebApplicationContext.SCOPE_APPLICATION)
  • 实现
    • API - ServletContextScope

# 自定义 Bean 作用域

  • 实现 Scope

    • org.springframework.beans.factory.config.Scope
  • 注册 Scope

    • API - org.springframework.beans.factory.config.ConfigurableBeanFactory#registerScope
  • 配置

    <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
      <property name="scopes">
        <map>
          <entry key="...">
          </entry>
        </map>
      </property>
    </bean>
    

# 问题

Spring 內建的 Bean 作用域有几种?

singleton、prototype、request、session、application 以及 websocket

singleton Bean 是否在一个应用是唯一的?

否。singleton bean 仅在当前 Spring IoC 容器(BeanFactory)中是单例对象。

application Bean 是否可以被其他方案替代?

可以的,实际上,“application” Bean 与“singleton” Bean 没有本质区别

# 参考资料

  • Spring 官方文档之 Core Technologies (opens new window)
  • 《小马哥讲 Spring 核心编程思想》 (opens new window)
📝 帮助改善此页面! (opens new window)
#Java#框架#Spring#Bean
上次更新: 2024/04/24, 07:33:33
Spring IoC 依赖来源
Spring Bean 生命周期

← Spring IoC 依赖来源 Spring Bean 生命周期→

最近更新
01
Spring MVC 之视图技术
02-17
02
Spring MVC 之跨域
02-16
03
Spring Web 应用
02-14
更多文章>
Theme by Vdoing | Copyright © 2019-2024 钝悟(dunwu) | CC-BY-SA-4.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×