springboot启动流程源码之一(new SpringApplication(primarySources))

我们首先来看看new SpringApplication()方法里面做了什么

在这里插入图片描述

/**
	 * Create a new {@link SpringApplication} instance. The application context will load
	 * beans from the specified primary sources (see {@link SpringApplication class-level}
	 * documentation for details. The instance can be customized before calling
	 * {@link #run(String...)}.
	 * @param resourceLoader the resource loader to use
	 * @param primarySources the primary bean sources
	 * @see #run(Class, String[])
	 * @see #setSources(Set)
	 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
    this.resourceLoader = resourceLoader;
    Assert.notNull(primarySources, "PrimarySources must not be null");
    this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
    //根据classpath中的存在的类推断应用类型
    this.webApplicationType = WebApplicationType.deduceFromClasspath();
    //获取ApplicationContextInitializer,并设置到Initializers中
    setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
    //获取ApplicationContextInitializer,并设置到Initializers中
    setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    //推断主类
    this.mainApplicationClass = deduceMainApplicationClass();
}

getSpringFactoriesInstances()

这个方法的作用应该就是获取指定类型的实例集合 。具体如何,还得继续往下面走走看。
在这里插入图片描述

private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
    ClassLoader classLoader = getClassLoader();
    // Use names and ensure unique to protect against duplicates
    //获取classPath所有META-INF/spring.factories中配置的key为ApplicationContextInitializer的接口
    Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
    //初始化所有配置ApplicationContextInitializer的接口的实现类
    List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
    AnnotationAwareOrderComparator.sort(instances);
    return instances;
}

SpringFactoriesLoader.loadFactoryNames()

SpringFactoriesLoader可以加载jar包下META-INF下的spring.factories,把相关接口的实现按照key,value的形式加载到内存,一个接口的多个实现可以按照","进行分割。对程序员来说,利用SpringFactoriesLoader可以加载自定义的ApplicationListener、ApplicationContextInitializer、Configuration类等,spring-boot的starter就是通过SpringFactoriesLoader加载了相关Configuration配置类。 

在这里插入图片描述

private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
    MultiValueMap<String, String> result = cache.get(classLoader);
    //判断当前classLoader是否已经加载过,加载过直接返回
    if (result != null) {
        return result;
    }

    try {
        //获取classPath中所有META-INF/spring.factories,可能需要递归到父classLoader
        Enumeration<URL> urls = (classLoader != null ?
                                 classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
                                 ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
        result = new LinkedMultiValueMap<>();
        //读取所有的url指定文件,保存到以文件中key为key的map(集中同一类型的实现类)
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            UrlResource resource = new UrlResource(url);
            Properties properties = PropertiesLoaderUtils.loadProperties(resource);
            for (Map.Entry<?, ?> entry : properties.entrySet()) {
                String factoryTypeName = ((String) entry.getKey()).trim();
                for (String factoryImplementationName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
                    result.add(factoryTypeName, factoryImplementationName.trim());
                }
            }
        }
        //保存到缓存
        cache.put(classLoader, result);
        return result;
    }
    catch (IOException ex) {
        throw new IllegalArgumentException("Unable to load factories from location [" +
                                           FACTORIES_RESOURCE_LOCATION + "]", ex);
    }
}

总结:
在new SpringApplication()中,要重点看一下getSpringFactoriesInstances()方法,主要用来获取实例类,加载META-INF/spring.factories文件。

关注公众号
在这里插入图片描述
每周会更新干货知识

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值