码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot源码(一)

时间:2019-12-23 00:10:21      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:null   argument   技术   app   none   进入   repo   service   figure   

今天又是新的一天,时间过的真快,我真(心)慌(慌)。加油,小菜??,?? !

先大致看一下,spring boot 启动到底都干了什么吧

// 启动入口方法
public static void main(String[] args) {
        SpringApplication.run(IchatApplication.class, args);
    }

进入run()方法

 public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
        return run(new Class[]{primarySource}, args);
    }

                                 ?? ?? ?? ?? ?? ?? ?? ??
 public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
        return (new SpringApplication(primarySources)).run(args);
    }

先看一下new SpringApplication(primarySources)

  
public SpringApplication(Class<?>... primarySources) {
        this((ResourceLoader)null, primarySources);
    }
                             ?? ?? ?? ?? ?? ?? ?? ??
    public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
        this.sources = new LinkedHashSet();
        this.bannerMode = Mode.CONSOLE;
        this.logStartupInfo = true;
        this.addCommandLineProperties = true;
        this.addConversionService = true;
        this.headless = true;
        this.registerShutdownHook = true;
        this.additionalProfiles = new HashSet();
        this.isCustomEnvironment = false;
        this.resourceLoader = resourceLoader;
        Assert.notNull(primarySources, "PrimarySources must not be null");
        this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
        // 设置应用类型 NONE,    SERVLET,    REACTIVE;三种类型
        this.webApplicationType = WebApplicationType.deduceFromClasspath();
        // 设置初始化器
        this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
        // 设置监听器
        this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
        // 
        this.mainApplicationClass = this.deduceMainApplicationClass();
    }

primarySources:
技术图片

public ConfigurableApplicationContext run(String... args) {
        // 计时
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
        this.configureHeadlessProperty();

        // 获取并启动监听器
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        listeners.starting();

        Collection exceptionReporters;
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            // 准备环境
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            this.configureIgnoreBeanInfo(environment);
            Banner printedBanner = this.printBanner(environment);
            // 创建Spring容器
            context = this.createApplicationContext();
            exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
            // Spring容器前置处理
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            // 刷新容器 
            this.refreshContext(context);
            // Spring容器后置处理
            this.afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
            }
            // 发出结束执行的事件
            listeners.started(context);
            //  执行Runners
            this.callRunners(context, applicationArguments);
        } catch (Throwable var10) {
            this.handleRunFailure(context, var10, exceptionReporters, listeners);
            throw new IllegalStateException(var10);
        }

        try {
            listeners.running(context);
            return context;
        } catch (Throwable var9) {
            this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
            throw new IllegalStateException(var9);
        }
    }

Spring Boot源码(一)

标签:null   argument   技术   app   none   进入   repo   service   figure   

原文地址:https://www.cnblogs.com/VVII/p/12081719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!