我们首先来看看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文件。
关注公众号
每周会更新干货知识