Spring Boot源码分析

1.通过源码分析SpringBoot的启动流程.首先找到启动程序的入口,从run方法点进去.

@SpringBootApplication
public class OrderApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class);
    }
    /**
     * 定义tomcat需要
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 注意这里要指向原先用main方法执行的Application启动类
        return builder.sources(OrderApplication.class);
    }
}

2.首先创建一个SpringApplication对象通过initialize(sources);方法进行初始化,sources是我们传入的主配置类.

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);
}

3.下面先看创建的SpringApplication对象

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.lazyInitialization = false;
    this.resourceLoader = resourceLoader;
    Assert.notNull(primarySources, "PrimarySources must not be null");
    this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
    this.webApplicationType = WebApplicationType.deduceFromClasspath();
    this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
    this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
    this.mainApplicationClass = this.deduceMainApplicationClass();
}

4.setInitializers 方法就是加载spring.factorys配置文件,获取里面的 ApplicationContextInitializer 找到这样的类进行初始化

5. setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));

6 .SpringApplication对象创建完以后,直接调用了run方法

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);
        context = this.createApplicationContext();
        exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
        this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
        this.refreshContext(context);
        this.afterRefresh(context, applicationArguments);
        stopWatch.stop();
        if (this.logStartupInfo) {
            (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
        }

        listeners.started(context);
        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);
    }
}

7.解析上面的run方法, StopWatch stopWatch = new StopWatch();

StopWatch stopWatch =new StopWatch();开启一个定时器,然后启动定时器,configureHeadlessProperty();这句的意思是开启java.awt.headless./关于这个模式,自行百度一下就可以清楚非常简单.紧接着获取监听器,启动监听器然后获得一个上下文参数,紧接着准备环境,点进去.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot源码分析涉及到多个类和方法。其中,方法org.springframework.boot.SpringApplication#prepareContext用于准备应用程序的上下文,并返回一个配置好的Spring应用程序上下文。 Spring Boot的主要目的是简化Spring框架中繁复的配置问题。它通过提供自动配置、约定优于配置和快速开发的原则来实现这一目标。具体来说,它使用了一系列机制来简化开发者的工作,包括自动配置、自动扫描、自动装配等。 在Spring Boot应用程序中,这些机制通常会在应用程序启动时加载到IOC容器中。例如,在一个典型的Spring Boot应用程序的入口类中,使用@SpringBootApplication注解标记了应用程序的主类,并使用@MapperScan注解指定了需要扫描的Mapper接口所在的包。在main方法中,通过调用SpringApplication.run方法来启动应用程序,并将主类和命令行参数传递给它。在运行过程中,Spring Boot会自动扫描应用程序中的注解和配置,并将相应的Bean加载到IOC容器中。 总的来说,Spring Boot源码分析涉及到Spring Boot的核心类和方法,以及它提供的各种机制和配置方式。通过深入研究这些源码,可以更好地理解Spring Boot的工作原理和内部机制。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [springboot源码分析](https://blog.csdn.net/xiaoxiaoyeyaya/article/details/108560836)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot源码分析](https://blog.csdn.net/mutf7/article/details/119387639)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值