下面列出了org.springframework.boot.WebApplicationType#SERVLET 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void initialize()
throws ContainerInitializationException {
Timer.start("SPRINGBOOT2_COLD_START");
SpringApplicationBuilder builder = new SpringApplicationBuilder(getEmbeddedContainerClasses())
.web(springWebApplicationType); // .REACTIVE, .SERVLET
if (springProfiles != null) {
builder.profiles(springProfiles);
}
applicationContext = builder.run();
if (springWebApplicationType == WebApplicationType.SERVLET) {
((AnnotationConfigServletWebServerApplicationContext)applicationContext).setServletContext(getServletContext());
AwsServletRegistration reg = (AwsServletRegistration)getServletContext().getServletRegistration(DISPATCHER_SERVLET_REGISTRATION_NAME);
if (reg != null) {
reg.setLoadOnStartup(1);
}
}
super.initialize();
initialized = true;
Timer.stop("SPRINGBOOT2_COLD_START");
}
private Class<?>[] getEmbeddedContainerClasses() {
Class<?>[] classes = new Class[2];
if (springWebApplicationType == WebApplicationType.REACTIVE) {
try {
// if HandlerAdapter is available we assume they are using WebFlux. Otherwise plain servlet.
this.getClass().getClassLoader().loadClass("org.springframework.web.reactive.HandlerAdapter");
log.debug("Found WebFlux HandlerAdapter on classpath, using reactive server factory");
classes[0] = ServerlessReactiveServletEmbeddedServerFactory.class;
} catch (ClassNotFoundException e) {
springWebApplicationType = WebApplicationType.SERVLET;
classes[0] = ServerlessServletEmbeddedServerFactory.class;
}
} else {
classes[0] = ServerlessServletEmbeddedServerFactory.class;
}
classes[1] = springBootInitializer;
return classes;
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationContextInitializedEvent) {
ApplicationContextInitializedEvent initialized = (ApplicationContextInitializedEvent) event;
ConfigurableApplicationContext context = initialized.getApplicationContext();
if (!(context instanceof GenericApplicationContext)) {
throw new IllegalStateException("ApplicationContext must be a GenericApplicationContext");
}
if (!isEnabled(context.getEnvironment())) {
return;
}
GenericApplicationContext generic = (GenericApplicationContext) context;
ConditionService conditions = initialize(generic);
functional(generic, conditions);
apply(generic, initialized.getSpringApplication(), conditions);
}
else if (event instanceof ApplicationEnvironmentPreparedEvent) {
ApplicationEnvironmentPreparedEvent prepared = (ApplicationEnvironmentPreparedEvent) event;
if (!isEnabled(prepared.getEnvironment())) {
return;
}
logger.info("Preparing application context");
SpringApplication application = prepared.getSpringApplication();
findInitializers(application);
WebApplicationType type = getWebApplicationType(application, prepared.getEnvironment());
Class<?> contextType = getApplicationContextType(application);
if (type == WebApplicationType.NONE) {
if (contextType == AnnotationConfigApplicationContext.class || contextType == null) {
application.setApplicationContextClass(GenericApplicationContext.class);
}
}
else if (type == WebApplicationType.REACTIVE) {
if (contextType == AnnotationConfigReactiveWebApplicationContext.class || contextType == null) {
application.setApplicationContextClass(ReactiveWebServerApplicationContext.class);
}
}
else if (type == WebApplicationType.SERVLET) {
if (contextType == AnnotationConfigServletWebServerApplicationContext.class || contextType == null) {
application.setApplicationContextClass(ServletWebServerApplicationContext.class);
}
}
}
}
public SpringBootProxyHandlerBuilder<RequestType> servletApplication() {
this.applicationType = WebApplicationType.SERVLET;
return self();
}