四、Spring IOC——容器启动的主要流程方法

一、Spring IOC容器启动的主要流程方法(AbstractApplicationContext中的refresh方法)
如果应用直接使用ApplicationContext,以FileSystemXmlApplicationContext为例,在代码实现中可以看到其构造方法:

public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable             ApplicationContext parent) throws BeansException {
    super(parent);
    setConfigLocations(configLocations);
    if (refresh) {
        refresh();
    }
}

refresh()函数的定义在AbstractApplicationContext中实现如下:

public void refresh() throws BeansException, IllegalStateException {
    synchronized (this.startupShutdownMonitor) {
        // 一、Prepare this context for refreshing.
        // 前戏,做容器刷新前的准备工作
        // 1、设置容器的启动时间
        // 2、设置活跃状态为true
        // 3、设置关闭状态为false
        // 4、获取Environment对象,并加载当前系统的属性值到Environment对象中
        // 5、准备监听器和事件的集合对象中,默认为空的集合
        prepareRefresh();

        // 二、Tell the subclass to refresh the internal bean factory.
        // 创建容器对象:DefaultListableBeanFactory
        // 加载xml配置文件的属性值到当前工厂中,最重要的就是BeanDefinition
        ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

        // 三、Prepare the bean factory for use in this context.
        // BeanFactory的准备工作,对各种属性进行填充
        prepareBeanFactory(beanFactory);
        try {
            //四、 Allows post-processing of the bean factory in context subclasses.
            // 子类覆盖方法做额外的处理,此处我们自己一般不做任何拓展工作,但是可以查看web中的代码,是有具体实现的
            postProcessBeanFactory(beanFactory);

            // 五、Invoke factory processors registered as beans in the context.
            // 调用各种BeanFactory处理器 =========各种注解扫描==========
            // 其中ConfigurationClassPostProcessor实现了@Configuration、@ComponentScan、    @ComponentScans、@Import、@ImportResource等注解的解析功能
            invokeBeanFactoryPostProcessors(beanFactory);

            // 六、Register bean processors that intercept bean creation.
            // 注册bean处理器,这里只是注册功能,真正调用的是getBean方法
            registerBeanPostProcessors(beanFactory);

            // 七、Initialize message source for this context.
            // 为上下文初始化message源,即国际化:不同语言的消息体
            initMessageSource();
            //八、 Initialize event multicaster for this context.
            // 初始化事件监听多路广播器
            initApplicationEventMulticaster();
            // 九、Initialize other special beans in specific context subclasses.
            // 留给子类来初始化其他的bean
            onRefresh();
            // 十、Check for listener beans and register them.
            // 在所有注册的bean中查找listener bean,注册到消息广播器中
            registerListeners();

            // 十一、Instantiate all remaining (non-lazy-init) singletons.
            // 初始化剩下的单实例(非懒加载的),==========很重要==========
            finishBeanFactoryInitialization(beanFactory);

            // 十二、Last step: publish corresponding event.
            // 完成刷新过程,通知声名周期处理器LifecycleProcessor刷新过程,同时发出ContextRefreshEvent通知事件
            finishRefresh();
        } catch (BeansException ex) {
            if (logger.isWarnEnabled()) {
                logger.warn("Exception encountered during context initialization - "cancelling refresh attempt: " + ex);
            }
            // 十三、Destroy already created singletons to avoid dangling resources.
            destroyBeans();
            //十四、 Reset 'active' flag.
            cancelRefresh(ex);
            // Propagate exception to caller.
            throw ex;
        } finally {
            // 十五、Reset common introspection caches in Spring's core, since we
            // might not ever need metadata for singleton beans anymore...
            resetCommonCaches();
        }
    }
}

可以看到,refresh()函数即IoC容器启动时的一系列复杂操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值