点击查看【processon】

    1. public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
    2. //配置source
    3. this.resourceLoader = resourceLoader;
    4. Assert.notNull(primarySources, "PrimarySources must not be null");
    5. this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
    6. //配置是否为web环境
    7. this.webApplicationType = WebApplicationType.deduceFromClasspath();
    8. //创建初始化构造器
    9. setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
    10. //创建应用监听
    11. setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    12. //配置应用的主要方法所在类
    13. this.mainApplicationClass = deduceMainApplicationClass();
    14. }
    • 配置是否为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”,

        1. "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; } ```