面试被问一致性hash?看这一篇就够了

167 阅读3分钟

自我思考

一。我认为的spring framework是什么?

首先,我认为 spring framework 是一个 基于 Java 的企业开发框架,它提供了大量的基础设施组件支持,如 依赖管理,web,dao,事件,aop等。其次,我认为 spring 还是 灵活扩展的框架,它提供了非常精细化的生命周期管理,每个生成周期的阶段都预留了不同的接口和钩子,供我们做一些定制化的实现。另外,我认为 spring framework还在不断得更迭,再往cloud方向发展,从springframwork,再到boot,再到cloud,再到为来的cloud native(graal vm),spring 对自己的定位还在不断的变化。目前来看,spring framework 已经是Java领域的开发框架事实标准。

二。为什么spring framework要提供如此多,如此精细的生命周期管理?

可以分为两方面回答这个问题,一个是官方的角度,还有一个是我自己的角度。

先是官方的角度。在spring framework的官方文档中,提到了spring的设计哲学,我认为有两点和此有关,原文如下。

Provide choice at every level. Spring lets you defer design decisions as late as possible. For example, you can switch persistence providers through configuration without changing your code. The same is true for many other infrastructure concerns and integration with third-party APIs.

Accommodate diverse perspectives. Spring embraces flexibility and is not opinionated about how things should be done. It supports a wide range of application needs with different perspectives.

第一点的意思是 spring 让开发人员 有尽可能多的选择,允许他们可以在尽可能晚的阶段通过spring的生命周期管理自己的程序。第二点是 spring在设计阶段是灵活的,具有包容性的。它支持在不同的程序周期视角做一些处理。

其次是我的观点。

议题探究

深入spring 生命周期管理

BeanFactory 中 bean 生命周期管理

核心组件
  • 核心工厂组件

    • BeanFactory
  • 容器组件

    • BeanPostProcessor

      • bean instance 相关

        如果考虑在 bean 实例方面做文章,可以考虑实现 BeanPostProcessor

      • InstantiationAwareBeanPostProcessor(核心类)-- 重点分析(bean在每个阶段做的事情?属性编辑)

        • 实例化前后容器介入
        • 属性编辑阶段
        • 初始化前后容器介入
      • DestructionAwareBeanPostProcessor

    • BeanFactoryPostProcessor -- 属于 Application Context,不是beanfactory使用

      • BeanDefinition 相关

        application context can detect the BeanFactoryPostProcessor bean,and load them before other beans be created

        bean定义前 的 前置操作

      • PropertyResourceConfigurer(属性编辑?)
  • bean组件

    • Aware

      • BeanNameAware -- 此阶段bean已经基本成型
      • BeanClassLoaderAware
      • BeanFactoryAware
    • InitializingBean

    • DisposableBean // destory

核心组件探究
BeanPostProcessor

bean 实例化及依赖注入完成阶段

  • org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization

    • 此过程,bean的属性和依赖已经填充(populate)完毕

      see : before - org.springframework.beans.factory.InitializingBean#afterPropertiesSet

  • org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization

    • FactoryBean,proxy,wrapper 相关

      see: after - org.springframework.beans.factory.InitializingBean#afterPropertiesSet

      see: after - init-method

InstantiationAwareBeanPostProcessor

bean 实例化及属性填充阶段

在精确属性被设置 和 依赖注入阶段之前

目标与作用

  • 创建目标代理:create proxies with special TargetSources (pooling targets,lazily initializing targets, etc)
  • 额外注入:implement additional injection strategies such as field injection.

注意事项:

  • 这个接口一般是 spring internal 使用
  • 建议实现 BeanPostProcessor 或者 derive InstantiationAwareBeanPostProcessorAdapter
  • org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation

    • before Instantiation

  • org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor#postProcessAfterInstantiation

    • after Instantiation ,before populate properties

      tips : ideal callback for performing custom field injection on the given bean instance

  • org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor#postProcessPropertyValues(重点方法)

    • 属性装配预处理

从两个角度剖析bean的生命周期

tips: 我认为spring 对 bean 的阶段处理分为 容器级 和 bean级 是如下原因:

容器级是对整体的容器内对象做统一的处理,是广义性的

bean级是对单位bean做阶段的细化处理,是狭义的

容器阶段 和 bean定义阶段 的 协作处理,构造出了 灵活可扩展的 bean 生命周期

BeanFactory Bean定义加载流程图