关于java:springboot源码解析管中窥豹系列之Initializer四

22次阅读

共计 8571 个字符,预计需要花费 22 分钟才能阅读完成。

一、前言

  • 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 duplicates
    Set<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 loadSpringFactories(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 factories 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 Loaders
org.springframework.boot.env.PropertySourceLoader=\
org.springframework.boot.env.PropertiesPropertySourceLoader,\
org.springframework.boot.env.YamlPropertySourceLoader

# Run Listeners
org.springframework.boot.SpringApplicationRunListener=\
org.springframework.boot.context.event.EventPublishingRunListener

# Error Reporters
org.springframework.boot.SpringBootExceptionReporter=\
org.springframework.boot.diagnostics.FailureAnalyzers

# Application Context Initializers
org.springframework.context.ApplicationContextInitializer=\
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 Listeners
org.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 Processors
org.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 Analyzers
org.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.InvalidConfigurationPropertyValueFailureAnalyzer

# FailureAnalysisReporters
org.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 : getInitializers()) {Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(initializer.getClass(),
                ApplicationContextInitializer.class);
        Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");
        initializer.initialize(context);
    }
}
  • 前两行判断类型
  • 最初一行回调执行

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

正文完
 0