zoukankan      html  css  js  c++  java
  • SpringBoot启动配置原理

    启动流程

      1、创建SpringApplication对象(springboot2.x是通过构造器创造的)

     1   @SuppressWarnings({ "unchecked", "rawtypes" })
     2     public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
     3         this.resourceLoader = resourceLoader;
           //先判断主配置类是否为空
    4 Assert.notNull(primarySources, "PrimarySources must not be null");
           //不为空保存主配置类
    5 this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
           // 判断当前是否是一个Web应用
    6 this.webApplicationType = WebApplicationType.deduceFromClasspath();
           // 从类路径下META-INF/spring.factories配置的所有ApplicationContextInitializer;然后保存起来
    7 setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
           // 从类路径下META-INF/spring.factories配置所有的ApplicationListener
    8 setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
           // 从多个配置类中找到有main方法的主配置类
    9 this.mainApplicationClass = deduceMainApplicationClass(); 10 }

      2、运行run方法

     1 /**
     2      * Run the Spring application, creating and refreshing a new
     3      * {@link ApplicationContext}.
     4      * @param args the application arguments (usually passed from a Java main method)
     5      * @return a running {@link ApplicationContext}
     6      */
     7     public ConfigurableApplicationContext run(String... args) {
     8         StopWatch stopWatch = new StopWatch();
     9         stopWatch.start();
    10         ConfigurableApplicationContext context = null;
    11         Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    12         configureHeadlessProperty();
           // 获取SpringApplicationRunListeners;从类路径下META-INF/spring.factories
    13 SpringApplicationRunListeners listeners = getRunListeners(args); 14 listeners.starting(); 15 try {
             //封装命令行参数  
    16 ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
             // 准备环境
    17 ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); 18 configureIgnoreBeanInfo(environment);
             // 打印图标
    19 Banner printedBanner = printBanner(environment);
             // 创建ApplicationContext 决定创建web的ioc还是普通的ioc 底层用的反射创建
    20 context = createApplicationContext(); 21 exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class, 22 new Class[] { ConfigurableApplicationContext.class }, context);
             // 准备上下文环境
    23 prepareContext(context, environment, listeners, applicationArguments, printedBanner);
             // ioc容器初始化 如果是web应用还会创建嵌入式的Tomcat
             // 扫描、创建、加载所有的组件
    24 refreshContext(context); 25 afterRefresh(context, applicationArguments); 26 stopWatch.stop(); 27 if (this.logStartupInfo) { 28 new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch); 29 } 30 listeners.started(context); 31 callRunners(context, applicationArguments); 32 } 33 catch (Throwable ex) { 34 handleRunFailure(context, ex, exceptionReporters, listeners); 35 throw new IllegalStateException(ex); 36 } 37 38 try { 39 listeners.running(context); 40 } 41 catch (Throwable ex) { 42 handleRunFailure(context, ex, exceptionReporters, null); 43 throw new IllegalStateException(ex); 44 }
           // 启动完成之后,返回ioc容器
    45 return context; 46 }
  • 相关阅读:
    Weex框架源码分析(Android)(一)
    Android之手机振动和振铃
    使用selenium实现模拟淘宝登陆
    Android—修改button属性
    一个ROS的服务,使机器人向前移动指定距离
    用python写一个百度翻译
    python的特殊数字类型(无穷大、无穷小等)
    ESP32 开发笔记(十二)LittlevGL 添加自定义字体和物理按键
    设计的不错的网站
    轻功到底有没有?
  • 原文地址:https://www.cnblogs.com/lxy-java/p/13038855.html
Copyright © 2011-2022 走看看