springboot源码解读

1.static run
2.。new SpringApplication()
~.initialize
     a. this.webEnvironment = deduceWebEnvironment();

推断当前环境是否为web应用环境

       b.   setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));

底层通过SpringFactoriesLoader.loadFactoryNames方法从当前classpath环境中加载META-INF/spring.factories配置文件,从文件中读取名称为ApplicationContextInitializer的相关配置信息并进行初始化程序的构建。

        c.setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));

底层通过SpringFactoriesLoader.loadFactoryNames方法从当前classpath环境中加载META-INF/spring.factories配置文件,从文件中读取名称为ApplicationListener的相关配置信息并进行监听器程序的构建。

        d.this.mainApplicationClass = deduceMainApplicationClass();

推断应用主类的名称。

3.run()

a.StopWatch stopWatch = new StopWatch();

启动秒表计算应用启动时间,用于在开发过程中方便程序员调试性能等,非线程安全,不建议用于生产。
。。。

b.configureHeadlessProperty();

对无屏幕,键盘等硬件的设备进行特殊的配置。配置使用Headless,对于只有远程登录使用的服务器来说这样性能要好一些

c.SpringApplicationRunListeners listeners = getRunListeners(args);

底层通过SpringFactoriesLoader.loadFactoryNames方法从当前classpath环境中加载META-INF/spring.factories配置文件,从文件中读取名称为SpringApplicationRunListener的相关配置信息并进行可用监听程序的构建。

最基本的Spring Boot程序中运行监听器只有一个org.springframework.boot.context.event.EventPublishingRunListener

d.listeners.starting();

调用运行监听器的starting方法。

最基本的Spring Boot程序中会执行org.springframework.boot.context.event.EventPublishingRunListener类的starting方法

此方法会执行之前获取的ApplicationListener集合并调用每个对象的onApplicationEvent方法。

e.ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);

设置启动参数对象,保存main方法入口参数,以备后续使用。

f.ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);

准备当前环境配置对象,用于读取当前环境配置文件

g.Banner printedBanner = printBanner(environment);

获取Spring Boot的横幅文字

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.7.RELEASE)
如果在classpath下存在banner.txt, banner.jpg, banner.gif文件,会覆盖Spring Boot自带的横幅文字

context = createApplicationContext();

根据当前环境创建Spring上下文核心应用对象org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext

analyzers = new FailureAnalyzers(context);

底层通过SpringFactoriesLoader.loadFactoryNames方法从当前classpath环境中加载META-INF/spring.factories配置文件,从文件中读取名称为FailureAnalyzer的相关配置信息并进行错误分析对象的构建。

如果启动出错了可以据此分析

prepareContext(context, environment, listeners, applicationArguments, printedBanner);

注册启动参数到bean工厂, 加载bean定义的来源并根据其中配置加载bean

refreshContext(context);

刷新当前Spring上下文环境对象,实现对象的解析与整合

afterRefresh(context, applicationArguments);

调用实现了CommandLineRunner, ApplicationRunner进口的类,在应用完成的时候执行自定义处理逻辑。

listeners.finished(context, null);

调用运行监听器的onApplicationEvent方法。

stopWatch.stop();

停止秒表,用于计时。

new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);

打印应用日志信息。

猜你喜欢

转载自blog.csdn.net/qq_40408317/article/details/80528568
今日推荐