spring容器分析

spring容器的顶级接口

org.springframework.context.Lifecycle	声明周期
org.springframework.beans.factory.Aware  资源导入
org.springframework.beans.factory.BeanFactory	对象读取
org.springframework.core.AliasRegistry 对象注册	
org.springframework.core.io.ResourceLoader	资源加载
org.springframework.core.io.InputStreamSource	资源
org.springframework.core.env.EnvironmentCapable	资源读取
org.springframework.context.MessageSource	消息
org.springframework.context.ApplicationEventPublisher	事件
org.springframework.core.Ordered  顺序

生命周期

org.springframework.context.Lifecycle
管理初始化关闭以及运行时声明周期状态

资源注入

org.springframework.beans.factory.Aware
资源注入的声明标记 没有任何信息 实际信息以其子接口声明

Aware (org.springframework.beans.factory)
ApplicationEventPublisherAware (org.springframework.context)
NotificationPublisherAware (org.springframework.jmx.export.notification)
MessageSourceAware (org.springframework.context)
BeanFactoryAware (org.springframework.beans.factory)
EnvironmentAware (org.springframework.context)
EmbeddedValueResolverAware (org.springframework.context)
ResourceLoaderAware (org.springframework.context)
ImportAware (org.springframework.context.annotation)
LoadTimeWeaverAware (org.springframework.context.weaving)
BeanNameAware (org.springframework.beans.factory)
BeanClassLoaderAware (org.springframework.beans.factory)
ApplicationContextAware (org.springframework.context)

容器输出

org.springframework.beans.factory.BeanFactory
基本容器根接口 定义spring容器中对象的读取标准

容器注入

org.springframework.core.AliasRegistry
容器资源注入的跟接口 定义了spring容器中对象的写入标准

资源读取

org.springframework.core.io.ResourceLoader
定义了容器读取外部资源的行为

资源流

org.springframework.core.io.InputStreamSource
定义了容器加载资源流行为的基本标准 基于此的实现最终均以流的形式输出

环境变量

org.springframework.core.env.EnvironmentCapable
定义了容器获取环境变量的标准 基于此的定制化实现 最终均以环境变量输出

消息

org.springframework.context.MessageSource

事件发布

org.springframework.context.ApplicationEventPublisher
定义了容器内事件发布标准 基于此的实现均具备发布事件的能力

序列号

org.springframework.core.Ordered
定义了内部对象获取序列号的能力 任何基于此的实现 均可在容器中获取到自己的序列号

。。。

从这几个顶级接口的能力来分析框架的结构
1、最初始的容器
集成了beanfactory和aliasRegisty的能力,提供了最基础的bean初始化和初始化之后对象访问的能力
2、引入插件Aware
任何一个给容器配置插件的能力接口均出自这个顶级接口之下,如果我们的容器需要多少对应的组件接入,增加一个插件即可
3、引入Lifecycle 生命周期管理
给容器增加生命周期的管理能力,提供容器初始化和销毁的支撑
4、容器的初始化引入资源管理
ResourceLoader、InputStreamSource引入加载器和流,在此基础上落地,就能给把期望的资源转换成容器需要的信息,不论资源是什么形式 xml、yml、URL 等等,都可以对应的实现解析器完成转换
5、容器是一个抽象的概念
org.springframework.context.support.AbstractApplicationContext 此对象为基础的抽象类,封装了spring容器的基本的框架能力。另一个角度来说 spring容器演进多年,代码不断完善,不建议一直针对某一部分细节死抠,先了解基础功能,然后将功能分区,复杂实现简单化,初步熟知当前架构的基础骨架。
基于上部分的抽象信息,我们可以初始化一个对象,将对应的能力接口进行整合,做出一个简版的容器,或许可以对后续深入了解打下良好的基础

。。。。

提及spring 看过源码的人可能基本上都会了解到这个方法refresh 此方法是spring容器初始化的一个标准机制,提供了标准的初始化流程,后续的任何一个人都可以通过定制一个org.springframework.context.support.AbstractApplicationContext的子类来进一步加工一个定制化的spring

    public void refresh() throws BeansException, IllegalStateException {
        synchronized (this.startupShutdownMonitor) {
            // Prepare this context for refreshing.
            prepareRefresh();
            // Tell the subclass to refresh the internal bean factory.
            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
            // Prepare the bean factory for use in this context.
            prepareBeanFactory(beanFactory);
            try {
                // Allows post-processing of the bean factory in context subclasses.
                postProcessBeanFactory(beanFactory);
                // Invoke factory processors registered as beans in the context.
                invokeBeanFactoryPostProcessors(beanFactory);
                // Register bean processors that intercept bean creation.
                registerBeanPostProcessors(beanFactory);
                // Initialize message source for this context.
                initMessageSource();
                // Initialize event multicaster for this context.
                initApplicationEventMulticaster();
                // Initialize other special beans in specific context subclasses.
                onRefresh();
                // Check for listener beans and register them.
                registerListeners();
                // Instantiate all remaining (non-lazy-init) singletons.
                finishBeanFactoryInitialization(beanFactory);
                // Last step: publish corresponding event.
                finishRefresh();
            }
            catch (BeansException ex) {
            }

            finally {
            }
        }
    }

以下是每一个方法的简单描述 每一步都给我们提供了无线的可能

1、标准化加载资源配置
2、初始化上下文容器factory

3、配置factory标准化组件和默认配置

4、定制化覆盖标配factory (默认无)
5、Invoke factory processors registered as beans in the context. (org.springframework.context.support.PostProcessorRegistrationDelegate)

6、Register bean processors that intercept bean creation. (org.springframework.context.support.PostProcessorRegistrationDelegate)
7、初始化 消息对象
8、初始化 事件
9、初始化 定制化信息
10、初始化 注册事件监听器
11、初始化 非延迟加载的对象 (遍历注册进来的对象信息,没有标记延迟的一律实例化)
12、最后完成善后工作发布事件
清理加载过程中的临时缓存
初始化生命周期
发布完成事件
注册容器查看器 org.springframework.context.support.LiveBeansView

。。。。。

容器的后续深加工
org.springframework.beans.factory.config.BeanFactoryPostProcessor
在上面的第5步会完成此对象实现的调用,基于此能力,可以完成容器初始化后的深加工,也可以重新初始化新容器。如果有这个诉求 ,那在初始化抽象容器之后 ,在容器标准初始化之前记着给他注入能力插件 。具体操作参照此配置上下文的标准说明 org.springframework.context.ConfigurableApplicationContext#addBeanFactoryPostProcessor

。。。。。。

对象的深加工
spring框架所有的能力都是围绕对象bean实现的 ,我们简单再介绍下容器中对象的深加工参考点
org.springframework.beans.factory.config.BeanPostProcessor
如果我们有对bean的实例化的相关诉求可以从这个地方做文章,上门容器标准初始化的第三步提供了此插件的插入, 我们可以将自己的定制化实现从这一步注册给容器。具体参考org.springframework.beans.factory.config.ConfigurableBeanFactory#addBeanPostProcessor

对象详细细节提供几个参考点的入口

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsAfterInitialization

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsBeforeInitialization

以上 简单介绍了spring的一部分顶级接口以及容器和对象的定制化插件以及执行点的标配,希望可以给自己和读者更多的灵感。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值