SpringBoot在SpringApplication创建对象实例的时候,会默认读取 "META-INF/spring.factories" 中信息,然后将该文件中的配置信息载入到Spring中
- 1.点击SpringApplication.run(HelloApplication.class, args);
- 2.SpringApplication的初始化方法
setInitializers((Collection) getSpringFactoriesInstances( ApplicationContextInitializer.class));
@SuppressWarnings({ "unchecked", "rawtypes" })
private void initialize(Object[] sources) {
if (sources != null && sources.length > 0) {
this.sources.addAll(Arrays.asList(sources));
}
this.webEnvironment = deduceWebEnvironment();
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}
- 3.在SpringFactoriesLoader加载方法中
SpringFactoriesLoader.loadFactoryNames(type, classLoader));
private <T> Collection<? extends T> getSpringFactoriesInstances(Class<T> type,
Class<?>[] parameterTypes, Object... args) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// Use names and ensure unique to protect against duplicates
Set<String> names = new LinkedHashSet<String>(
SpringFactoriesLoader.loadFactoryNames(type, classLoader));
List<T> instances = createSpringFactoriesInstances(type, parameterTypes,
classLoader, args, names);
AnnotationAwareOrderComparator.sort(instances);
return instances;
}
- 4.读取"META-INF/spring.factories"文件
classLoader.getResources("META-INF/spring.factories")
public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
String factoryClassName = factoryClass.getName();
try {
Enumeration urls = (classLoader != null)
? classLoader.getResources("META-INF/spring.factories")
: ClassLoader.getSystemResources("META-INF/spring.factories");
List result = new ArrayList();
while (urls.hasMoreElements()) {
URL url = (URL) urls.nextElement();
Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
String factoryClassNames = properties.getProperty(factoryClassName);
result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames)));
}
return result;
} catch (IOException ex) {
throw new IllegalArgumentException("Unable to load [" + factoryClass.getName()
+ "] factories from location [" + "META-INF/spring.factories" + "]", ex);
}
}
- 5.查看"META-INF/spring.factories"文件
- 1.在SpringBoot中有一个Jar包:spring-boot
- 2.在SpringBoot中有一个Jar包:spring-boot-autoconfigure