AI智能
改变未来

springboot源码解析-管中窥豹系列之Initializer(四)

一、前言

  • Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去。
  • 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot源码管中窥豹系列。

二、Initializer

  • 上一节我们介绍了Runner,它是在项目加载完成之后执行的
  • 有后就有前,有没有在项目加载之前执行的呢?

我们今天介绍的ApplicationContextInitializer就是在spring的bean加载之前执行的

public interface ApplicationContextInitializer<C extends ConfigurableApplicationContext> {void initialize(C applicationContext);}
  • 使用很简单,实现ApplicationContextInitializer接口就可以了
  • 它是先于普通bean加载的,所以不能用@Component的方式
  • 具体怎么被springboot加载,往下看,我们分析源码的时候说

三、源码解析

如果对springboot源码一点都不了解的,推荐先看第一节:整体架构

1、获取ApplicationContextInitializer

我们直接先看SpringApplication的构造方法

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {...setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));...}

我们先看setInitializers方法,再看里面的getSpringFactoriesInstances方法

public void setInitializers(Collection<? extends ApplicationContextInitializer<?>> initializers) {this.initializers = new ArrayList<>(initializers);}
  • 很简单,把查询的initializers集合赋值到本地变量上
  • 接着看getSpringFactoriesInstances方法,这个initializers集合怎么查的
private <T> Collection<T> getSpringFactoriesInstances(Class<T> type) {return getSpringFactoriesInstances(type, new Class<?>[] {});}private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {ClassLoader classLoader = getClassLoader();// Use names and ensure unique to protect against duplicatesSet<String> names = new LinkedHashSet<>(SpringFactoriesLoader.loadFactoryNames(type, classLoader));List<T> instances = createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);AnnotationAwareOrderComparator.sort(instances);return instances;}
  • (1) 获取ApplicationContextInitializer实现类的名称集合
  • (2) 加载成实例instances
  • (3) 排序,返回

我们先研究下SpringFactoriesLoader.loadFactoryNames(type, classLoader)这个方法:

public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {String factoryTypeName = factoryType.getName();return l56coadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());}private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {MultiValueMap<String, String> result = cache.get(classLoader);if (result != null) {return result;}try {Enumeration<URL> urls = (classLoader != null ?classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));result = new LinkedMultiValueMap<>();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 fa56cctories from location [\" +FACTORIES_RESOURCE_LOCATION + \"]\", ex);}}
  • 注意:factoryType就是我们传入的参数 ApplicationContextInitializer.class
  • 这里有个新的map结构:MultiValueMap<String, String>,它和Map<String, List>是一样的
  • 我们先看一下这里:classLoader.getResources(FACTORIES_RESOURCE_LOCATION)
public static final String FACTORIES_RESOURCE_LOCATION = \"META-INF/spring.factories\";
  • 加载所有的META-INF/spring.factories,按接口名称放入MultiValueMap<String, String>,并cache
  • 注意,这类文件不止一个,它们分布在不同的jar包里面
  • 这么说你可能不懂,我们看一下这类文件长什么样,我们看一个系统自带的
# PropertySource Loadersorg.springframework.boot.env.PropertySourceLoader=\\org.springframework.boot.env.PropertiesPropertySourceLoader,\\org.springframework.boot.env.YamlPropertySourceLoader# Run Listenersorg.springframework.boot.SpringApplicationRunListener=\\org.springframework.boot.context.event.EventPublishingRunListener# Error Reportersorg.springframework.boot.SpringBootExceptionReporter=\\org.springframework.boot.diagnostics.FailureAnalyzers# Application Context Initializersorg.springframework.context.Apad8plicationContextInitializer=\\org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\\org.springframework.boot.context.ContextIdApplicationContextInitializer,\\org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\\org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer,\\org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer# Application Listenersorg.springframework.context.ApplicationListener=\\org.springframework.boot.ClearCachesApplicationListener,\\org.springframework.boot.builder.ParentContextCloserApplicationListener,\\org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\\org.springframework.boot.context.FileEncodingApplicationListener,\\org.springframework.boot.context.config.AnsiOutputApplicationListener,\\org.springframework.boot.context.config.ConfigFileApplicationListener,\\org.springframework.boot.context.config.DelegatingApplicationListener,\\org.springframework.boot.context.logging.ClasspathLoggingApplicationListener,\\org.springframework.boot.context.logging.LoggingApplicationListener,\\org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener# Environment Post Processorsorg.springframework.boot.env.EnvironmentPostProcessor=\\org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\\org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor,\\org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor,\\org.springframework.boot.reactor.DebugAgentEnvironmentPostProcessor# Failure Analyzersorg.springframework.boot.diagnostics.FailureAnalyzer=\\org.springframework.boot.diagnostics.analyzer.BeanCurrentlyInCreationFailureAnalyzer,\\org.springframework.boot.diagnostics.analyzer.BeanDefinitionOverrideFailureAnalyzer,\\org.springframework.boot.diagnostics.analyzer.BeanNotOfRequiredTypeFailureAnalyzer,\\org.springframework.boot.diagnostics.analyzer.BindFailureAnalyzer,\\org.springframework.boot.diagnostics.analyzer.BindValidationFailureAnalyzer,\\org.springframework.boot.diagnostics.analyzer.UnboundConfigurationPropertyFailureAnalyzer,\\org.springframework.boot.diagnostics.analyzer.ConnectorStartFailureAnalyzer,\\org.springframework.boot.diagnostics.analyzer.NoSuchMethodFailureAnalyzer,\\org.springframework.boot.diagnostics.analyzer.NoUniqueBeanDefinitionFailureAnalyzer,\\org.springframework.boot.diagnostics.analyzer.PortInUseFailureAnalyzer,\\org.springframework.boot.diagnostics.analyzer.ValidationExceptionFailureAnalyzer,\\org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyNameFailureAnalyzer,\\org.springframework.boot.diagnostics.analyzer.InvalidConfigurationPropertyValueFailureAad0nalyzer# FailureAnalysisReportersorg.springframework.boot.diagnostics.FailureAnalysisReporter=\\org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter
  • 接口 = 实现类1,实现类2,实现类3
  • 我们假如有了自己的ApplicationContextInitializer实现类,我们在resource下面新建/META-INF/spring.factories文件,按上面的格式写上就可以被加载了
org.springframework.context.ApplicationContextInitializer=\\org.my.zb.MyApplicationContextInitializer
  • 我们把思维拉回去,讲完了怎么取的实现类名称集合
  • 回去看 createSpringFactoriesInstances();
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;}
  • (1) 获取Class
  • (2) 获取构造函数
  • (3) 利用反射新建instance对象
  • (4) 加入集合

至此,我们就得到了:List instances

2、执行ApplicationContextInitializer

我们看SpringApplication的run方法:

public ConfigurableApplicationContext run(String... args) {...try {...prepareContext(context, environment, listeners, applicationArguments, printedBanner);...}catch (Throwable ex) {...}...return context;}

进入到prepareContext方法:

private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment,SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {...applyInitializers(context);...}

定位到了applyInitializers():

protected void applyInitializers(ConfigurableApplicationContext context) {for (ApplicationContextInitializer initializer : ge11e1tInitializers()) {Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(initializer.getClass(),ApplicationContextInitializer.class);Assert.isInstanceOf(requiredType, context, \"Unable to call initializer.\");initializer.initialize(context);}}
  • 前两行判断类型
  • 最后一行回调执行
  • 目录:
    springboot源码解析-管中窥豹系列

欢迎关注公众号:丰极,更多技术学习分享。

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » springboot源码解析-管中窥豹系列之Initializer(四)