美文网首页程序员
springboot初始化过程(源代码注释)

springboot初始化过程(源代码注释)

作者: qming_c | 来源:发表于2018-02-05 01:17 被阅读0次
    package com.lsb.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    

    springboot启动入口类

    //SpringApplication.run()
    public static ConfigurableApplicationContext run(Object source, String... args) {
            return run(new Object[]{source}, args);
        }
    
    public static ConfigurableApplicationContext run(Object[] sources, String[] args) {
            return (new SpringApplication(sources)).run(args);
        }
    

    调用了SpringApplication.run方法


    //new SpringApplication(sources)
    //一些基本配置的初始化
    public SpringApplication(Object... sources) {
    //banner就是springboot启动时的那个小图案...
            this.bannerMode = Mode.CONSOLE;
            this.logStartupInfo = true;
            this.addCommandLineProperties = true;
            this.headless = true;
            this.registerShutdownHook = true;
            this.additionalProfiles = new HashSet();
            this.initialize(sources);
        }
    

    对一些基本的配置进行初始化,

    • 输出图案
    • 是否打印启动log
    • 命令行参数读取?
    • Headless模式是系统的一种配置模式。在该模式下,系统缺少了显示设备、键盘或鼠标
    • Java程序经常也会遇到进程挂掉的情况,一些状态没有正确的保存下来,这时候就需要在JVM关掉的时候执行一些清理现场的代码。JAVA中的ShutdownHook提供了比较好的方案。
    • 额外的参数
    • 初始化
    //初始化
    private void initialize(Object[] sources) {
            if (sources != null && sources.length > 0) {
                this.sources.addAll(Arrays.asList(sources));
            }
    //判断是不是web环境,还是标准环境
    
            this.webEnvironment = deduceWebEnvironment();
            setInitializers((Collection) getSpringFactoriesInstances(//
                    ApplicationContextInitializer.class));
            setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    //判断是不是从main方发中执行的程序
            this.mainApplicationClass = deduceMainApplicationClass();
        }
    
    • 此函数主要是对执行环境的一些判断以及初始化工作


      sources
    ("javax.servlet.Servlet",   "org.springframework.web.context.ConfigurableWebApplicationContext")
    

    启动器通过反射判断有没有这两个类的的实现,如果有就是web环境,否则就是普通环境

    //从函数调用栈中寻找main方法,如果找到,就把main方法的类对象返回
    private Class<?> deduceMainApplicationClass() {
            try {
                StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
                for (StackTraceElement stackTraceElement : stackTrace) {
                    if ("main".equals(stackTraceElement.getMethodName())) {
                        return Class.forName(stackTraceElement.getClassName());
                    }
                }
            }
            catch (ClassNotFoundException ex) {
                // Swallow and continue
            }
            return null;
        }
    
    • 通过函数调用栈判断是不是从main方法中开始执行的陈股

    //run
    public ConfigurableApplicationContext run(String... args) {
    //定时器,用于日志打印程序执行时间
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
    //上下文
            ConfigurableApplicationContext context = null;
    //失败分析工具
            FailureAnalyzers analyzers = null;
    //Headless模式是系统的一种配置模式。在该模式下,系统缺少了显示设备、键盘或鼠标。(java.awt.headless)
            configureHeadlessProperty();
            SpringApplicationRunListeners listeners = getRunListeners(args);
            listeners.starting();
            try {
    //main方法参数实例化
                ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
                ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
    //打印springboot图案
                Banner printedBanner = printBanner(environment);
                context = createApplicationContext();
                analyzers = new FailureAnalyzers(context);
                prepareContext(context, environment, listeners, applicationArguments,
                        printedBanner);
                refreshContext(context);
                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初始化过程(源代码注释)

        本文链接:https://www.haomeiwen.com/subject/aaovzxtx.html