public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {//配置sourcethis.resourceLoader = resourceLoader;Assert.notNull(primarySources, "PrimarySources must not be null");this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));//配置是否为web环境this.webApplicationType = WebApplicationType.deduceFromClasspath();//创建初始化构造器setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));//创建应用监听setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));//配置应用的主要方法所在类this.mainApplicationClass = deduceMainApplicationClass();}
配置是否为web环境 ```java /**
- The application should not run as a web application and should not start an
- embedded web server.
- 该应用程序不应作为 Web 应用程序运行,也不应启动嵌入式网络服务器。
*/ NONE,
/**
- The application should run as a servlet-based web application and should start an
- embedded servlet web server.
该应用程序应作为基于 servlet 的 Web 应用程序运行,并应启动嵌入式 servlet Web 服务器。 */ SERVLET,
/**
- The application should run as a reactive web application and should start an
- embedded reactive web server.
该应用程序应作为响应式 Web 应用程序运行,并应启动嵌入式响应式 Web 服务器。 */ REACTIVE;
private static final String[] SERVLET_INDICATOR_CLASSES = { “javax.servlet.Servlet”,
"org.springframework.web.context.ConfigurableWebApplicationContext" };
private static final String WEBMVC_INDICATOR_CLASS = “org.springframework.web.servlet.DispatcherServlet”;
private static final String WEBFLUX_INDICATOR_CLASS = “org.springframework.web.reactive.DispatcherHandler”;
private static final String JERSEY_INDICATOR_CLASS = “org.glassfish.jersey.servlet.ServletContainer”;
private static final String SERVLET_APPLICATION_CONTEXT_CLASS = “org.springframework.web.context.WebApplicationContext”;
private static final String REACTIVE_APPLICATION_CONTEXT_CLASS = “org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext”;
static WebApplicationType deduceFromClasspath() { if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null) && !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null) && !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)) { return WebApplicationType.REACTIVE; } for (String className : SERVLET_INDICATOR_CLASSES) { if (!ClassUtils.isPresent(className, null)) { return WebApplicationType.NONE; } } return WebApplicationType.SERVLET; } ```
