SpringBoot之自动配置原理

本文详细解析了SpringBoot的自动配置原理,包括启动主程序、容器类型的推断、从spring.factories加载Bean信息,以及创建和刷新ApplicationContext的过程。
摘要由CSDN通过智能技术生成

一、定义

二、自动配置原理

1、前置准备

启动main主程序

2、大体流程

3、具体过程

  1. SpringApplication.run(App.class,args)
    1. SpringApplication run(new Class<?>[] { primarySource }, args)
      1. new SpringApplication(primarySources).run(args)
        1. new SpringApplication(primarySources)(创建并初始化SpringApplication实例,App.class是容器的入口配置类)
          1. SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources)
            1. this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
            2. this.webApplicationType = WebApplicationType.deduceFromClasspath();(推断容器类型,默认是WebApplicationType.SERVLET)
            3. this.bootstrapRegistryInitializers = new ArrayList<>(getSpringFactoriesInstances(BootstrapRegistryInitializer.class));(从cache或者/META-INF/spring.factories文件里面读取BootstrapRegistryInitializer的bean信息,该bean信息实例化后可以用来初始化BootstrapRegistry实例)
              1. SpringApplication getSpringFactoriesInstances(Class<T> type) 
                1. SpringApplication getSpringFactoriesInstances(type, new Class<?>[] {})
                  1. ClassLoader classLoader = getClassLoader();
                  2. Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
                    1. SpringFactoriesLoader loadFactoryNames
                      1. SpringFactoriesLoader loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList())
                        1. SpringFactoriesLoader loadSpringFactories(ClassLoader classLoader)
                          1. Map<String, List<String>> result = cache.get(classLoader)(先尝试从SpringFactoriesLoader的静态变量cache中获取
                          2. Enumeration<URL> urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION)(上面的cache获取不到数据,则会通过getResources方法加载FACTORIES_RESOURCE_LOCATION路径的文件,FACTORIES_RESOURCE_LOCATION表示META-INF/spring.factories)
                          3. String[] factoryImplementationNames =StringUtils.commaDelimitedListToStringArray((String) entry.getValue());(由于entry.getValue的结果值是一个由逗号拼接的字符串,所以需要通过该commaDelimitedListToStringArray把字符串转为字符串数组)
                          4. SpringFactoriesLoader cache.put(classLoader, result);(把上面的字符串数组放入SpringFactoriesLoader的静态变量cache
                  3. List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names)
            4. SpringApplication setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));(从cache或者META-INF/spring.factories文件里面读取ApplicationContextInitializer的bean信息,该bean信息实例化后可以用来初始化ConfigurableApplicationContext实例)
            5. SpringApplication setListeners((Collection)getSpringFactoriesInstances(ApplicationListener.class));(从cache或者META-INF/spring.factories文件里面读取ApplicationListener的bean信息,这些bean信息是容器预留的事件监听器)
            6. this.mainApplicationClass = deduceMainApplicationClass();(找到并设置程序主类)
        2. run(String... args)
          1. ConfigurableApplicationContext context = null
          2. context = createApplicationContext()(创建容器
            1. this.applicationContextFactory.create(this.webApplicationType);
              1. ApplicationContextFactory.DEFAULT
                1. SERVLET(容器Web类型是Servlet)
                  1. new AnnotationConfigServletWebServerApplicationContext()
          3. prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
          4. refreshContext(context);
            1. refresh(context)(刷新容器
          5. afterRefresh(context, applicationArguments);
          6. callRunners(context, applicationArguments)
             
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值