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 }
  • 相关阅读:
    shell脚本(多线程批量创建用户)
    听说你在从事前端开发?那这10个JavaScript的优化问题你不得不知道!
    解读网易易盾新一代IoT安全编译器Maze
    Java Web学习总结(12)——使用Session防止表单重复提交
    自动化内存管理和引用计数
    关于烂代码的那些事(下)
    rest_framwork序列化Serializer和ModelSerializer、url生成HyperlinkedIdentityField、深度depth、局部钩子校验
    版本控制
    解析器:request.body、request.POST、request.data
    权限之model对象权限,查看所有对象时,过滤指定对象。操作单条对象时,判断有没有操作权限
  • 原文地址:https://www.cnblogs.com/lxy-java/p/13038855.html
Copyright © 2011-2022 走看看