前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot源码解读

springboot源码解读

作者头像
老梁
发布2019-09-10 16:53:58
4580
发布2019-09-10 16:53:58
举报

springboot源码从main函数开始

代码语言:javascript
复制
public static void main(String[] args) {
    ApplicationContext app = SpringApplication.run(BeikbankApplication.class, args);
}

进入run方法,再持续进入到实际操作的步骤

代码语言:javascript
复制
public static ConfigurableApplicationContext run(Object source, String... args) {
    return run(new Object[] { source }, args);
}
代码语言:javascript
复制
public static ConfigurableApplicationContext run(Object[] sources, String[] args) {
    return new SpringApplication(sources).run(args);
}
代码语言:javascript
复制
public ConfigurableApplicationContext run(String... args) {
    //秒表计时器,用来记录整个容器启动时间
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    FailureAnalyzers analyzers = null;
    //配置headless系统属性
    configureHeadlessProperty();
    //获取springbootApplication注解的类信息开启监听
    SpringApplicationRunListeners listeners = getRunListeners(args);
    listeners.starting();
    try {
        //应用参数初始化
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(
                args);
        //环境变量初始化
        ConfigurableEnvironment environment = prepareEnvironment(listeners,
                applicationArguments);
        //打印banner,就是spring图形
        Banner printedBanner = printBanner(environment);
        //创建应用上下文,默认会得到AnnotationConfigEmbeddedWebApplicationContext
        context = createApplicationContext();
        //反射加载实例化FailureAnalyzer实现类
        analyzers = new FailureAnalyzers(context);
        //上下文环境准备
        prepareContext(context, environment, listeners, applicationArguments,
                printedBanner);
        //主要的IOC过程,也是springMVC的主要过程,进行bean的相关接口初始化和bean实例化
        refreshContext(context);
        //到上一步tomcat已经启动完成了,这里算是springboot的扩展接口,可以自己实现ApplicationRunner或CommandLineRunner接口,在这里进行实例化开始运行
        afterRefresh(context, applicationArguments);
        //发布事件监听器
        listeners.finished(context, null);
        //停止计时
        stopWatch.stop();
        if (this.logStartupInfo) {
            new StartupInfoLogger(this.mainApplicationClass)
                    .logStarted(getApplicationLog(), stopWatch);
        }
        return context;
    }
    catch (Throwable ex) {
        handleRunFailure(context, listeners, analyzers, ex);
        throw new IllegalStateException(ex);
    }
}

我把主要步骤的每一步都标明了注释,看起来这里很多都是springboot特有,但主要的实现还是对springMVC的继承,你去看springmvc的源码,会发现,它主要的过程就是refreshContext这个方法所展现的过程

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-10-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • springboot源码从main函数开始
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档