上一篇【springboot高级】(一:自定义容器初始化器的三种方式,实现ApplicationContextInitializer接口。(使用篇))我们讲到了,注册自定义应用初始化器的三种方式:
那么我们这篇从源码的角度来看一下他们是如何注册进去并且执行的。
首先我们都知道SpringApplication.run()方法是springboot项目的入口,那么我们就跟着run方法进去:
发现它的内部是先创建SpringApplication的实例,然后在继续run。
// 1、run进来,
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
return run(new Class[]{primarySource}, args);
}
// 2、继续run,这里他是先new了一个SpringApplication实例,然后再继续run.
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
return (new SpringApplication(primarySources)).run(args);
}
// 3、然后我们再继续看一下他在创建实例的时候做了啥,
public SpringApplication(Class<?>... primarySources) {
this((ResourceLoader)null, primarySources);
}
// 4、发现他调用的是另一个构造函数,这个构造函数,应该是做了很多事情了。
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
this.webApplicationType = WebApplicationType.deduceFromClasspath();
// 这就是我们的主角了
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}
从上面我们可以看出,SpringApplication在new实例的时候,调用了setInitializers方法,initializers是一个属性,就是调用了initializers的set方法,那么参数就应该是所以的自定义的初始化器了。
看参数:(Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class),调用getSpringFactoriesInstances方法。
// 1、调用,继续调用另一个重载方法
private <T> Collection<T> getSpringFactoriesInstances(Class<T> type) {
return getSpringFactoriesInstances(type, new Class<?>[] {});
}
// 2、继续调用,这一步有一些操作
private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
// 获取类加载器(加载类路劲下面的资源)
ClassLoader classLoader = getClassLoader();
// Set集合,使用set是为了防止重复,获取所有的自定义容器初始化器。
Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
// 拿到所有的初始化器,并且对其创建实例
List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
// 将所有实例进行排序
AnnotationAwareOrderComparator.sort(instances);
return instances;
}
到这里,最后面的这个方法,通过类加载器加载所有的自定义初始化器 -> 然后将所有的初始化器进行实例化 -> 并且排序。
让我们逐一击破:
1、获取所有的自定义初始化器:
// 1 、new LinkedHashSet,然后通过构造器传入Collection集合。
Set<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
// 2、核心方法
SpringFactoriesLoader.loadFactoryNames(type, classLoader)
// 3 加载FactoryNames
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
// factoryType就是我们上面最开始传进来的ApplicationContextInitializer.class,也就是我们自定义实现的接口。获取到类名
String factoryTypeName = factoryType.getName();
return (List)loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
}
// 4、加载SpringFactories
(List)loadSpringFactories(classLoader)
private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
// 获取缓存,如果有,那么直接返回,没有就从新获取,这里是为了防止重复获取
MultiValueMap<String, String> result = cache.get(classLoader);
if (result != null) {
return result;
}
try {
// public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories"
// 这里通过类加载器,去加载classpath下面的"META-INF/spring.factories"文件,如果没有那么就加载系统资源下面的"META-INF/spring.factories"
Enumeration<URL> urls = (classLoader != null ?
classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
result = new LinkedMultiValueMap<>();
// 然后循环,讲所有的含有META-INF/spring.factories的读取出来。并且放入cache中,
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);
}
}
2、拿到所有的初始化器,并且对其创建实例
List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
private <T> List<T> createSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes,
ClassLoader classLoader, Object[] args, Set<String> names) {
List<T> instances = new ArrayList<>(names.size());
for (String name : names) {
try {
// 通过反射,创建示例
Class<?> instanceClass = ClassUtils.forName(name, classLoader);
Assert.isAssignable(type, instanceClass);
Constructor<?> constructor = instanceClass.getDeclaredConstructor(parameterTypes);
T instance = (T) BeanUtils.instantiateClass(constructor, args);
// 并且添加进数组
instances.add(instance);
}
catch (Throwable ex) {
throw new IllegalArgumentException("Cannot instantiate " + type + " : " + name, ex);
}
}
return instances;
}
3、将所有实例进行排序
AnnotationAwareOrderComparator.sort(instances);
// 这个排序调用的是原生list的排序方法
public static void sort(List<?> list) {
if (list.size() > 1) {
list.sort(INSTANCE);
}
}
// INSTANCE:一个定义好的排序规则
public static final AnnotationAwareOrderComparator INSTANCE = new AnnotationAwareOrderComparator();
// 这个排序规则集成自OrderComparator,本身没有compare方法,使用的是父类的
public class AnnotationAwareOrderComparator extends OrderComparator {}
// OrderComparator的排序方法:
@Override
public int compare(@Nullable Object o1, @Nullable Object o2) {
return doCompare(o1, o2, null);
}
// 通过
private int doCompare(@Nullable Object o1, @Nullable Object o2, @Nullable OrderSourceProvider sourceProvider) {
boolean p1 = (o1 instanceof PriorityOrdered);
boolean p2 = (o2 instanceof PriorityOrdered);
if (p1 && !p2) {
return -1;
}
else if (p2 && !p1) {
return 1;
}
// 通过对应@Order注解的值来排序,具体有一些设计模式在里面,这里就不细说了。
int i1 = getOrder(o1, sourceProvider);
int i2 = getOrder(o2, sourceProvider);
return Integer.compare(i1, i2);
}
上面这些都是获取到我们自定义的初始化器,那么他是在什么地方执行的呢?
我的查找方式是(不清楚springboot整体流程的情况下):
找到initializers属性的get方法(他要执行,一定会调用initializers的get方法,不然没有地方获取到,而且也不能说是通过反射来获取他)
最后回退的结果是到达run方法。run方法比较复杂,这里就不细说了,
// get方法
public Set<ApplicationContextInitializer<?>> getInitializers() {
return asUnmodifiableOrderedSet(this.initializers);
}
// 回退
protected void applyInitializers(ConfigurableApplicationContext context) {
for (ApplicationContextInitializer initializer : getInitializers()) {
Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(initializer.getClass(),
ApplicationContextInitializer.class);
Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");
initializer.initialize(context);
}
}
// 再回退
private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment,
SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {
context.setEnvironment(environment);
postProcessApplicationContext(context);
applyInitializers(context);
listeners.contextPrepared(context);
if (this.logStartupInfo) {
logStartupInfo(context.getParent() == null);
logStartupProfileInfo(context);
}
// Add boot specific singleton beans
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
if (printedBanner != null) {
beanFactory.registerSingleton("springBootBanner", printedBanner);
}
if (beanFactory instanceof DefaultListableBeanFactory) {
((DefaultListableBeanFactory) beanFactory)
.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
}
if (this.lazyInitialization) {
context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
}
// Load the sources
Set<Object> sources = getAllSources();
Assert.notEmpty(sources, "Sources must not be empty");
load(context, sources.toArray(new Object[0]));
listeners.contextLoaded(context);
}
// 继续回退, 到达run方法。
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
// 这个方法执行的
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
然后我们重点看一下applyInitializers这个方法:
protected void applyInitializers(ConfigurableApplicationContext context) {
for (ApplicationContextInitializer initializer : getInitializers()) {
Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(initializer.getClass(),
ApplicationContextInitializer.class);
// 判断是否是实例,如果不是实例,就截断报异常
Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");
// 然后调用初始化方法,就是我们自定义实现的方法了。
initializer.initialize(context);
}
}