SpringBoot 源码解析

SpringBoot 源码解析


SpringApplication.java

public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
    this.webApplicationType = WebApplicationType.deduceFromClasspath();//判断当前类是WEB servlet
    this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));//加载所有的初始化器
    this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));//加载所有的监听器
    this.mainApplicationClass = this.deduceMainApplicationClass();//设置程序运行的主类
}
public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();//创建计时器,开始计时
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
    this.configureHeadlessProperty();//handless系统属性设置
    SpringApplicationRunListeners listeners = this.getRunListeners(args);//初始化监听器
    listeners.starting();//启动所有的监听器

    Collection exceptionReporters;
    try {
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);//设置命令行参数
        ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);//创建环境配置参数web/standard,设置环境监听器
        this.configureIgnoreBeanInfo(environment);
        Banner printedBanner = this.printBanner(environment);//打印banner图,即spring boot启动时的图
        context = this.createApplicationContext();//创建应用程序上下文
	 //准备异常报告器
        exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
        this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);//准备上下文环境 1监听配置 2设置环境参数 3初始化操作 4load对象的资源 加载启动类
        this.refreshContext(context);//refresh调用过程和spring一样(AbstractApplicationContext),自动装配,tomcat配置
        this.afterRefresh(context, applicationArguments);//留给用户扩展
        stopWatch.stop();//计时器停止
        if (this.logStartupInfo) {
            (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
        }

        listeners.started(context);
        this.callRunners(context, applicationArguments);//发布应用上下文启动,调用Runners执行器,发布上下文就绪时间,springboot启动完成
    } catch (Throwable var10) {
        
    }

    try {
        listeners.running(context);
        return context;
    } catch (Throwable var9) {
        
    }
}

springboot包和springboot autoconfig 包下游spring.factories配置文件
springboot autoconfig下的spring.factories里这个配置包含了自动注解

org.springframework.boot.autoconfigure.EnableAutoConfiguration

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值