springbootApplicationContextInitializer的三种使⽤⽅法⼩结⽬录
ApplicationContextInitializer的三种使⽤⽅法
概述
1、使⽤spring.factories⽅式
2、application.properties添加配置⽅式
3、直接通过add⽅法
ApplicationContextInitializer都⼲了些什么
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, 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 = Property(PROPERTY_NAME);
List<Class<?>> classes = new ArrayList<Class<?>>();
if (StringUtils.hasLength(classNames)) {
for (String className : kenizeToStringArray(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.t.config.DelegatingApplicationContextInitializer
@Override
public void initialize(ConfigurableApplicationContext context) {
ConfigurableEnvironment environment = Environment();
/**
* 初始化环境变量中的context.initializer.classes指定的类
**/
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.t.ContextIdApplicationContextInitializer
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ContextId contextId = getContextId(applicationContext);
applicationContext.Id());
/**
* 注册⼀个ContextId对象
**/
}
org.t.ConfigurationWarningsApplicationContextInitializer
@Override
public void initialize(ConfigurableApplicationContext context) {
/**
* 注⼊ComponentScan检查处理对象
**/
context.addBeanFactoryPostProcessor(new ConfigurationWarningsPostProcessor(getChecks()));
}
org.springframework.t.RSocketPortInfoApplicationContextInitializer
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
/**
* 注⼊⼀个端⼝检查和设置的,对应的事件RSocketServerInitializedEvent
**/
applicationContext.addApplicationListener(new Listener(applicationContext));
spring启动流程面试回答}
org.springframework.t.ServerPortInfoApplicationContextInitializer
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
/**
注册了⼀个监听org.springframework.t.WebServerInitializedEvent事件的,⽤于设置端⼝信息
**/
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事件的⽤于打印⾃动配置后的⽇志信息
}
}
所有的这些初始化类都没偶进⾏启动服务的实质性操作,都是通过注册对象,埋点,后⾯invokeBeanFactoryPostProcessors才真正调⽤初始化⽅法,⽽且在项⽬启动之前
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论