Spring IOC 初始化刷新流程一:prepareRefresh()

Spring IOC 初始化刷新流程:https://www.cnblogs.com/jhxxb/p/13609289.html

 

方法源码

public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext {
    protected void prepareRefresh() {
        // Switch to active.
        // 记录容器启动时间,然后设立对应的标志位
        this.startupDate = System.currentTimeMillis();
        this.closed.set(false);
        this.active.set(true);

        // 打印 info 日志:开始刷新容器
        if (logger.isDebugEnabled()) {
            if (logger.isTraceEnabled()) {
                logger.trace("Refreshing " + this);
            } else {
                logger.debug("Refreshing " + getDisplayName());
            }
        }

        // Initialize any placeholder property sources in the context environment.
        // 扩展方法,由子类去实现,可以在验证之前,为系统属性设置一些值
        // 这里是 AnnotationConfigApplicationContext,什么都没做
        initPropertySources();

        // Validate that all properties marked as required are resolvable: see ConfigurablePropertyResolver#setRequiredProperties
        // 这里有两步,getEnvironment(),然后验证系统环境中是否有 RequiredProperties 参数值。其实就干了一件事,验证是否存在必须的属性
        getEnvironment().validateRequiredProperties();

        // Store pre-refresh ApplicationListeners...
        // 存储容器预刷新的 ApplicationListeners
        if (this.earlyApplicationListeners == null) {
            this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
        } else {
            // Reset local application listeners to pre-refresh state.
            this.applicationListeners.clear();
            this.applicationListeners.addAll(this.earlyApplicationListeners);
        }

        // Allow for the collection of early ApplicationEvents, to be published once the multicaster is available...
        // 初始化容器,用于装载早期的一些事件
        this.earlyApplicationEvents = new LinkedHashSet<>();
    }

 

getEnvironment()

顶层接口位于:EnvironmentCapable,有如下实现,ConfigurableApplicationContext 是接口,所以容器的实现只有 AbstractApplicationContext

 

posted @ 2020-12-07 14:14  江湖小小白  阅读(355)  评论(0编辑  收藏  举报