ApplicationContextInitializer的三种使用方法
概述
ApplicationContextInitializer是在springboot启动过程(refresh方法前)调用,主要是在ApplicationContextInitializer中initialize方法中拉起了ConfigurationClassPostProcessor这个类(我在springboot启动流程中有描述),通过这个processor实现了beandefinition。
言归正传,ApplicationContextInitializer实现主要有3中方式:
1、使用spring.factories方式
首先我们自定义个类实现了ApplicationContextInitializer,然后在resource下面新建/META-INF/spring.factories文件。
public class Demo01ApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
System.out.println("user add method ==> ApplicationContextInitializer");
}
}
这个加载过程是在SpringApplication中的getSpringFactoriesInstances()方法中直接加载并实例后执行对应的initialize方法。代码如下:
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;
}
2、application.properties添加配置方式
对于这种方式是通过DelegatingApplicationContextInitializer这个初始化类中的initialize方法获取到application.properties中context.initializer.classes对应的类并执行对应的initialize方法。
只需要将实现了ApplicationContextInitializer的类添加到application.properties即可。如下:
下面我们看看DelegatingApplicationContextInitializer是如何加载的。看代码:
private static final String PROPERTY_NAME = "context.initializer.classes";
private List<Class<?>> getInitializerClasses(ConfigurableEnvironment env) {
String classNames = env.getProperty(PROPERTY_NAME);
List<Class<?>> classes = new ArrayList<Class<?>>();
if (StringUtils.hasLength(classNames)) {
for (String className : StringUtils.tokenizeToStringArray(classNames, ",")) {
classes.add(getInitializerClass(className));
}
}
return classes;
}
是从配置文件中获取到对应的初始化类信息,然后执行初始化方法。
3、直接通过add方法
这个方法就比较简单,直接在springboot启动的时候,add一个实现了ApplicationContextInitializer的类即可,代码如下:
@SpringBootApplication
public class InitializerDemoApplication {
public static void main(String[] args) {
//type01
SpringApplication springApplication = new SpringApplication(InitializerDemoApplication.class);
springApplication.addInitializers(new Demo01ApplicationContextInitializer());
springApplication.run(args);
//SpringApplication.run(InitializerDemoApplication.class,args);
}
}
以上3中方法都可以实现自定义的Initializer,只不过执行的顺序有差异。这里我比较感兴趣有2个,一个通过spring.factories实现SPI模式,有兴趣的可以看下jdbc-starter等一些相关springboot starter。
第二个就是作为一个钩子去拉起来"一坨"的bean。
ApplicationContextInitializer都干了些什么
初始化方法
org.springframework.boot.context.config.DelegatingApplicationContextInitializer
@Override
public void initialize(ConfigurableApplicationContext context) {
ConfigurableEnvironment environment = context.getEnvironment();
List<Class<?>> initializerClasses = getInitializerClasses(environment);
if (!initializerClasses.isEmpty()) {
applyInitializerClasses(context, initializerClasses);
}
}
也就是说没有定义的话,就不会初始化了。
org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.addBeanFactoryPostProcessor(new CachingMetadataReaderFactoryPostProcessor());
}
org.springframework.boot.context.ContextIdApplicationContextInitializer
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ContextId contextId = getContextId(applicationContext);
applicationContext.setId(contextId.getId());
applicationContext.getBeanFactory().registerSingleton(ContextId.class.getName(), contextId);
}
org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer
@Override
public void initialize(ConfigurableApplicationContext context) {
context.addBeanFactoryPostProcessor(new ConfigurationWarningsPostProcessor(getChecks()));
}
org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.addApplicationListener(new Listener(applicationContext));
}
org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.addApplicationListener(this);
}
org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
this.applicationContext = applicationContext;
applicationContext.addApplicationListener(new ConditionEvaluationReportListener());
if (applicationContext instanceof GenericApplicationContext) {
// Get the report early in case the context fails to load
// 注册一个监听ApplicationEvent事件的监听器用于打印自动配置后的日志信息
this.report = ConditionEvaluationReport.get(this.applicationContext.getBeanFactory());
}
}
所有的这些初始化类都没偶进行启动服务的实质性操作,都是通过注册对象,埋点,后面invokeBeanFactoryPostProcessors才真正调用初始化方法,而且在项目启动之前
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。