//获取所有的加载配置,加载了一个类,就是标注了@SpringBootApplication的这样一个类<br>public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {<br> ClassLoader classLoaderToUse = classLoader;<br> if (classLoaderToUse == null) {<br> classLoaderToUse = SpringFactoriesLoader.class.getClassLoader();//spring工厂的加载<br> }<br> String factoryTypeName = factoryType.getName();//获取类的名字<br> return loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList());//获取默认的包名,断言不为空<br> }<br>
loadSpringFactories():标注了@SpringBootApplication这样一个类,获取类的名字、获取默认的包名、断言不为空。首先会加载classload,这个classload标注了Application主启动类里面有@EnableAutoConfiguration;<br>
//枚举去遍历一个url,url从类加载器中获取所有的资源,获取所有的系统资源,有一个while循环,这个url判断有没有更多的元素,如果有就把它放到url里面,然后把url加载到properties里面,最后所有的东西都放到配置类里面去了,所有的资源都加载到配置类中<br>private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) {<br> Map<String, List<String>> result = cache.get(classLoader);<br> if (result != null) {<br> return result;<br> }<br><br> result = new HashMap<>();<br> try {<br> Enumeration<URL> urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION);//枚举去遍历一个url,url从类加载器中获取所有的资源<br> while (urls.hasMoreElements()) {//有一个while循环,这个url判断有没有更多的元素,如果有就把它放到url里面,然后把url加载到properties里面,最后所有的东西都放到配置类里面去了<br> URL url = urls.nextElement();<br> UrlResource resource = new UrlResource(url);<br> Properties properties = PropertiesLoaderUtils.loadProperties(resource);//所有的资源都加载到配置类中<br> for (Map.Entry<?, ?> entry : properties.entrySet()) {<br> String factoryTypeName = ((String) entry.getKey()).trim();<br> String[] factoryImplementationNames =<br> StringUtils.commaDelimitedListToStringArray((String) entry.getValue());<br> for (String factoryImplementationName : factoryImplementationNames) {<br> result.computeIfAbsent(factoryTypeName, key -> new ArrayList<>())<br> .add(factoryImplementationName.trim());<br> }<br> }<br> }<br><br> // Replace all lists with unmodifiable lists containing unique elements<br> result.replaceAll((factoryType, implementations) -> implementations.stream().distinct()<br> .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)));<br> cache.put(classLoader, result);<br> }<br> catch (IOException ex) {<br> throw new IllegalArgumentException("Unable to load factories from location [" +<br> FACTORIES_RESOURCE_LOCATION + "]", ex);<br> }<br> return result;<br> }<br>
//获取项目资源<br>classLoader.getResources(FACTORIES_RESOURCE_LOCATION);
FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories":加载了META-INF/spring.factories这个文件下的所有东西,从这里获取配置文件<br>
Properties properties = PropertiesLoaderUtils.loadProperties(resource);//所有的资源都加载到配置类中<br>
从这些资源中遍历了所有的nextElement(自动配置),遍历完成之后,封装为properties供我们使用:枚举去遍历一个url,url从类加载器中获取所有的资源,有一个while循环,这个url判断有没有更多的元素,如果有就把它放到url里面,然后把url加载到properties里面,最后所有的东西都放到配置类里面去了,所有的资源都加载到配置类中<br>