Bean的作用域

简介: Bean 常见的 6 种作用域

目录

1.作用域定义

2.Bean 的 6 种作用域

singleton

prototype

request

session

application(了解)

websocket(了解)

单例作⽤域(singleton) VS 全局作⽤域(application)

3. 设置作⽤域


1.作用域定义

限定程序中变量的可⽤范围叫做作⽤域,或者说在源代码中定义变量的某个区域就叫做作⽤域。

⽽ Bean 的作⽤域是指 Bean 在 Spring 整个框架中的某种⾏为模式,⽐如 singleton 单例作⽤域,就表示 Bean 在整个 Spring 中只有⼀份,它是全局共享的,那么当其他⼈修改了这个值之后,那么另⼀个⼈读取到的就是被修改的值。

2.Bean 的 6 种作用域

Spring 容器在初始化⼀个 Bean 的实例时,同时会指定该实例的作⽤域。Spring常见的有 6 种作⽤域,最后四种是基于 Spring MVC ⽣效的:

    1. singleton:单例作⽤域
    2. prototype:原型作⽤域(多例作⽤域)
    3. request:请求作⽤域
    4. session:会话作⽤域
    5. application:全局作⽤域
    6. websocket:HTTP WebSocket 作⽤域

    注意后 4 种状态是 Spring MVC 中的值,在普通的 Spring 项⽬中只有前两种

    singleton

      • 官⽅说明:(Default) Scopes a single bean definition to a single object instance for each Spring IoC container.
      • 描述:该作⽤域下的Bean在IoC容器中只存在⼀个实例:获取Bean(即通过applicationContext.getBean等⽅法获取)及装配Bean(即通过@Autowired注⼊)都是同⼀对象。
      • 场景:通常⽆状态的Bean使⽤该作⽤域。⽆状态表示Bean对象的属性状态不需要更新
      • 备注:Spring默认选择该作⽤域

      prototype

        • 官⽅说明:Scopes a single bean definition to any number of object instances.
        • 描述:每次对该作⽤域下的Bean的请求都会创建新的实例:获取Bean(即通过applicationContext.getBean等⽅法获取)及装配Bean(即通过@Autowired注⼊)都是新的对象实例。
        • 场景:通常有状态的Bean使⽤该作⽤域

        request

          • 官⽅说明:Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
          • 描述:每次http请求会创建新的Bean实例,类似于prototype
          • 场景:⼀次http的请求和响应的共享Bean
          • 备注:限定SpringMVC中使⽤

          session

            • 官⽅说明:Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
            • 描述:在⼀个http session中,定义⼀个Bean实例
            • 场景:⽤户回话的共享Bean, ⽐如:记录⼀个⽤户的登陆信息
            • 备注:限定SpringMVC中使⽤

            application(了解)

              • 官⽅说明:Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
              • 描述:在⼀个http servlet Context中,定义⼀个Bean实例
              • 场景:Web应⽤的上下⽂信息,⽐如:记录⼀个应⽤的共享信息
              • 备注:限定SpringMVC中使⽤

              websocket(了解)

                • 官⽅说明:Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.
                • 描述:在⼀个HTTP WebSocket的⽣命周期中,定义⼀个Bean实例
                • 场景:WebSocket的每次会话中,保存了⼀个Map结构的头信息,将⽤来包裹客户端消息头。第⼀次初始化后,直到WebSocket结束都是同⼀个Bean。
                • 备注:限定Spring WebSocket中使⽤

                单例作⽤域(singleton) VS 全局作⽤域(application)

                  • singleton 是 Spring Core 的作⽤域;application 是 Spring Web 中的作⽤域;
                  • singleton 作⽤于 IoC 的容器,⽽ application 作⽤于 Servlet 容器。

                  3. 设置作⽤域

                  使⽤ @Scope 标签就可以⽤来声明 Bean 的作⽤域,⽐如设置 Bean 的作⽤域,如下代码所示:

                  @Component
                  public class Users {
                   @Scope("prototype")
                   @Bean(name = "u1")
                   public User user1() {
                   User user = new User();
                   user.setId(1);
                   user.setName("Java"); // 【重点:名称是 Java】
                   return user;
                   }
                  }

                  image.gif

                  @Scope 标签既可以修饰⽅法也可以修饰类,@Scope 有两种设置⽅式:

                    1. 直接设置值:@Scope("prototype")
                    2. 使⽤枚举设置:@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
                    相关文章
                    |
                    5月前
                    |
                    前端开发 Java 开发者
                    Bean的生命周期和作用域
                    Bean的生命周期和作用域
                    |
                    11月前
                    |
                    XML Java 数据格式
                    Spring中Bean的作用域与生命周期
                    Spring中Bean的作用域与生命周期
                    53 0
                    |
                    5月前
                    |
                    XML Java 数据格式
                    Spring框架学习 -- Bean的生命周期和作用域
                    Spring框架学习 -- Bean的生命周期和作用域
                    45 2
                    |
                    5月前
                    |
                    存储 设计模式 Java
                    Bean 作用域和生命周期
                    Bean 作用域和生命周期
                    |
                    5月前
                    |
                    缓存 前端开发 Java
                    面试题:说说你对Bean作用域的理解和使用?
                    面试题:说说你对Bean作用域的理解和使用?
                    42 0
                    |
                    5月前
                    |
                    Java Spring
                    spring Bean的作用域和生命周期
                    spring Bean的作用域和生命周期
                    |
                    存储 安全 Java
                    Bean 的作用域和生命周期
                    Bean 的作用域和生命周期
                    66 1
                    |
                    XML Java Maven
                    springBean的作用域
                    springBean的作用域
                    65 3
                    |
                    开发框架 安全 Java
                    为什么Spring中每个Bean都要定义作用域
                    前面的视频中都有提到过Spring Bean的作用域。本期视频呢,我针对Spring Bean作用域做一个详细的解答。关于Spring Bean的作用域,我一共分为两个部分来介绍。首先,介绍Spring Bean作用域的定义,然后,介绍Spring为什么要定义作用域?
                    76 0
                    |
                    XML 安全 Java
                    Bean作用域和生命周期
                    Bean作用域和生命周期