其中有个 BootstrapRegistry 接口只是简单提及,本篇就详细分析一下 BootstrapRegistry 接口,这对于我们后续理解 《BootstrapRegistry 初始化器实现》的内容至关重要 在 《BootstrapRegistryInitializer 详解》 的 2.1 小节,我们对 BootstrapRegistry 进行了初步的介绍:它是一个用于存储和共享对象的注册表,这些对象在 ApplicationContext 2.1 源码初识首先让我们来看看 BootstrapRegistry 的源码:public interface BootstrapRegistry { <T> void register(Class 2.7.4 of 静态方法该静态方法是一个工厂方法,用于为给定实例创建一个BootstrapRegistry.InstanceSupplier。 三、总结本篇 Huazie 带大家通读了 BootstrapRegistry 的相关源码,这些内容对于后面的源码学习至关重要。
2.1 BootstrapRegistry 在《BootstrapRegistry 详解》 中,Huazie 详细介绍了 BootstrapRegistry 的源码,这有助于下面介绍的 BootstrapRegistry 2.3 BootstrapRegistry 初始化器实现 2.3.1 定义 DemoBootstrapper 下面我们来定义一个类 DemoBootstrapper,该类实现 BootstrapRegistryInitializer DemoBootstrapper implements BootstrapRegistryInitializer { @Override public void initialize(BootstrapRegistry 至于 initialize 方法中该添加哪些逻辑,这就要看它的参数 BootstrapRegistry 接口了。 三、总结 本篇 Huazie 介绍了如何自定义 BootstrapRegistry 初始化器实现,其中演示如何在引导上下文中注册了自定义的对象以及如何在引导上下文中添加引导上下文关闭事件监听器。
《Spring Boot 源码学习系列》 一、引言 上一篇博文《BootstrapRegistry 初始化器实现》,Huazie 向大家介绍了如何自定义 BootstrapRegistryInitializer 2.2 BootstrapRegistry 初始化器实现 在开始讲解 BootstrapContext 的实际使用场景之前,我们需要首先通过 BootstrapRegistry 初始化器实现类 注册自定义的对象 这块内容,有需要了解的朋友,请翻看 Huazie 的上一篇博文《BootstrapRegistry 初始化器实现》,这里不再赘述。 其实在 2.2 小节介绍的《BootstrapRegistry 初始化器实现》中,我们已经介绍了如何添加 BootstrapContext 关闭事件 监听器,这里就不再赘述。 DemoApplication.class); // 关闭 Banner打印 springApplication.setBannerMode(Banner.Mode.OFF); // 添加 BootstrapRegistry
《Spring Boot 源码学习系列》一、引言书接前文《BootstrapRegistry 详解》,在介绍 BootstrapRegistry 的内部类 InstanceSupplier 的 get >> instanceSuppliers :这是个 HashMap,它的键是 Class 对象,值是 InstanceSupplier 对象【它是 BootstrapRegistry 中一个内部接口类, 2.3.2 实现 BootstrapRegistry 接口中的方法在 《BootstrapRegistry 详解》中,我们已经了解相关的 5 个方法,下面直接看 DefaultBootstrapContext isRegistered 方法比较特殊,它在 BootstrapRegistry 和 BootstrapContext 接口中均可以看到。 下篇 Huazie 将通过自定义 BootstrapRegistry 初始化器实现,来看看引导上下文在 Spring Boot 的启动引导过程中的作用。
类图如下: 1、BootstrapRegistry接口 一个简单的对象注册表,在启动和处理环境配置期间可用,直到ApplicationContext准备好为止。 public interface BootstrapRegistry { // 注册特定类型到注册表。如果指定的类型已注册且未以单例形式获取,则将替换。 BootstrapContextClosedEvent(this, applicationContext)); } } 三、BootstrapRegistryInitializer 1、作用及触发时机 回调接口,用于在BootstrapRegistry void initialize(BootstrapRegistry registry); } 引导注册组件初始化器BootstrapRegistryInitializer在SpringApplication MyBootstrapRegistryInitializer implements BootstrapRegistryInitializer { @Override public void initialize(BootstrapRegistry
BootstrapRegistryInitializer 接口的源码:@FunctionalInterfacepublic interface BootstrapRegistryInitializer { void initialize(BootstrapRegistry BootstrapRegistryInitializer 接口只定义了一个 initialize 方法,该方法只有一个参数是 BootstrapRegistry;BootstrapRegistry 是一个简单的对象注册表 简而言之,BootstrapRegistry 是一个用于存储和共享对象的注册表,这些对象在ApplicationContext 准备好之前就可能已经被创建并需要被共享。 这里的 bootstrapContext 其实就是 BootstrapRegistry 注册表的一个默认实现 DefaultBootstrapContext 。
4、bootstrapRegistryInitializers BootstrapRegistryInitializer:回调接口,用于 BootstrapRegistry 初始化。 BootstrapRegistry:对象注册器,作用期间为从应用启动,Environment 处理直到 ApplicationContext 完备。 2、创建启动上下文 DefaultBootstrapContext = createBootstrapContext(); BootstrapContext:启动上下文,生命周期同 BootstrapRegistry DefaultBootstrapContext 继承了 BootstrapContext、BootstrapRegistry。 用于 BootstrapRegistry 初始化。
failed;**通知所有的监听器 failed public interface Bootstrapper { /** * Initialize the given {@link BootstrapRegistry * @param registry the registry to initialize */ void intitialize(BootstrapRegistry registry
而这里的 createBootstrapContext 方法就是用于创建默认的引导上下文对象 DefaultBootstrapContext,并利用 BootstrapRegistry 初始化器初始化该引导上下文对象 想深入了解的朋友们,可查看 Huazie 下面列出的博文: 《BootstrapRegistry 详解》 《深入 BootstrapContext 及其默认实现》 《BootstrapRegistry
1.5 添加 BootstrapRegistry 初始化器实现在 Huazie 的《BootstrapRegistryInitializer 详解》中,介绍了 加载和初始化 BootstrapRegistryInitializer 通过 SpringApplication 的 addBootstrapRegistryInitializer 方法,我们可以在 bootstrapRegistryInitializers 中添加额外的 BootstrapRegistry
argumentResolver.and(ConfigurableBootstrapContext.class, bootstrapContext); // // 向argumentResolver对象中添加BootstrapRegistry.class this.loader.load方法加载EnvironmentPostProcessor类型的对象,参数为argumentResolver argumentResolver = argumentResolver.and(BootstrapRegistry.class
spring.factories加载ApplicationListener类实例 8、从堆栈中推断出主应用程序类 BootstrapRegistryInitializer:该接口的作用是将一些默认的组件注册到BootstrapRegistry
@FunctionalInterface public interface BootstrapRegistryInitializer { void initialize(BootstrapRegistry