ApplicationContextFactory
是Spring Boot从2.4.0引入的供SpringApplication
使用用于创建ConfigurableApplicationContext
的策略工厂接口。
ApplicationContextFactory
接口定义了一个根据Web应用类型(WebApplicationType
)创建ConfigurableApplicationContext
的方法,其定义如下:
package org.springframework.boot;
@FunctionalInterface
public interface ApplicationContextFactory {
ConfigurableApplicationContext create(WebApplicationType webApplicationType);
}
除了上述的接口方法,ApplicationContextFactory
还提供了两个可以直接使用的静态方法和一个默认的实现:
ofContextClass()
:该方法通过BeanUtils.instantiateClass()
方法创建给定类型ConfigurableApplicationContext
的一个实例; static ApplicationContextFactory ofContextClass(Class<? extends ConfigurableApplicationContext> contextClass) {
return of(() -> BeanUtils.instantiateClass(contextClass));
}
of(Supplier)
: 该方法是对Java 8流式编程的支持。 static ApplicationContextFactory of(Supplier<ConfigurableApplicationContext> supplier) {
return (webApplicationType) -> supplier.get();
}
DEFAULT
实现根据WebApplicationType
的类型创建不同的ConfigurableApplicationContext
实例,基对应关系如下:WebApplicationType | ConfigurableApplicationContext |
---|---|
SERVLET |
AnnotationConfigServletWebServerApplicationContext |
REACTIVE |
AnnotationConfigReactiveWebServerApplicationContext |
default |
AnnotationConfigApplicationContext |
ApplicationContextFactory DEFAULT = (webApplicationType) -> {
try {
switch (webApplicationType) {
case SERVLET:
return new AnnotationConfigServletWebServerApplicationContext();
case REACTIVE:
return new AnnotationConfigReactiveWebServerApplicationContext();
default:
return new AnnotationConfigApplicationContext();
}
} catch (Exception ex) {
throw new IllegalStateException("Unable create a default ApplicationContext instance, "
+ "you may need a custom ApplicationContextFactory", ex);
}
};