SpringBoot源码分析(九)

293 阅读1分钟

SpringBoot源码分析(九)

finishRefresh 方法

当前代码的位置在 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#finishRefresh
@Override
protected void finishRefresh() {
    super.finishRefresh();
    //启动web服务,调用tomcat的start方法
    WebServer webServer = startWebServer();
    if (webServer != null) {
        //发送web服务初始化完成事件
        publishEvent(new ServletWebServerInitializedEvent(webServer, this));
    }
}

resetCommonCaches 方法

protected void resetCommonCaches() {
    //清除一些缓存
    ReflectionUtils.clearCache();
    AnnotationUtils.clearCache();
    ResolvableType.clearCache();
    CachedIntrospectionResults.clearClassLoader(getClassLoader());
}

至此整个refreshContext方法就到此结束,大部分核心内容都在这个方法完成,再回到rnu方法中看下callRunners方法

callRunners 方法

private void callRunners(ApplicationContext context, ApplicationArguments args) {
    List<Object> runners = new ArrayList<>();
    //获取所有的ApplicationRunner实例对象
    runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
    //获取所有的CommandLineRunner实例对象
    runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
    AnnotationAwareOrderComparator.sort(runners);
    //遍历调用对应的回调方法
    for (Object runner : new LinkedHashSet<>(runners)) {
        if (runner instanceof ApplicationRunner) {
            callRunner((ApplicationRunner) runner, args);
        }
        if (runner instanceof CommandLineRunner) {
            callRunner((CommandLineRunner) runner, args);
        }
    }
}

最后在发送一个应用准备完毕事件,整个springboot程序就启动完成