67.原理解析-SpringApplication创建初始化流程

springboot的创建初始化流程

  • 传递主程序类
    在这里插入图片描述

创建spring应用
在这里插入图片描述
在这里插入图片描述

this.resourceLoader = resourceLoader; 
//资源加载器
Assert.notNull(primarySources, "PrimarySources must not be null");
//断言,如果没有主程序类则打印"xxx" 
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
//保存主配置类信息
this.webApplicationType = WebApplicationType.deduceFromClasspath();
//决定当前web应用的类型 利用Class.utils
this.bootstrapRegistryInitializers = new ArrayList<>();
//初始启动器  List<BootstrapRegistryInitializer> 
进入WebApplicationType.deduceFromClasspath()
  • 首先判断是否为响应式编程
  • 判断为原生servlet编程

在这里插入图片描述

== 判断完毕,继续执行 ==

    this.bootstrapRegistryInitializers = new ArrayList<>(
getSpringFactoriesInstances(BootstrapRegistryInitializer.class));
    //去spring.factories文件中找org.springframework.boot.Bootstrapper
    //bootstrappers:初始化启动引导器
    setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
    //去spring.factories文件中找ApplicationContextInitializer
    setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    //去spring.factories文件中找应用监听器ApplicationListener
    this.mainApplicationClass = deduceMainApplicationClass();

getSpringFactoriesInstances方法就是去spring.factories文件中找相应的配置文件

    private <T> Collection<T> getSpringFactoriesInstances(Class<T> type) {
        return getSpringFactoriesInstances(type, new Class<?>[] {});
    }

    private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
        ClassLoader classLoader = getClassLoader();
        //类加载器
        // Use names and ensure unique to protect against duplicates
        Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
        List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
        AnnotationAwareOrderComparator.sort(instances);
        return instances;
    }

七个初始化器

  • 在这里插入图片描述
  • 将初始化器,监听器,引导器保存完毕就设定主程序
    在这里插入图片描述
  • for循环的意义在于:在栈中找有main方法的类

至此,springApplication初始化创建完毕

posted @ 2022-08-09 16:22  随遇而安==  阅读(43)  评论(0编辑  收藏  举报