a start logic analysis springboot ------- new SpringApplication ()

	SpringApplication public (<?> the ResourceLoader ResourceLoader, Class ... primarySources) { 
		this.resourceLoader = ResourceLoader; 
		Assert.notNull (primarySources, "Not PrimarySources MUST BE null"); 
          this.primarySources a LinkedHashSet new new = <> (Arrays.asList ( primarySources));
. // (. 1) application type inferred based on the class of the classpath present this.webApplicationType WebApplicationType.deduceFromClasspath = ();
          . // (2) Get ApplicationContextInitializer, and to set the Initializers setInitializers ((Collection) getSpringFactoriesInstances ( ApplicationContextInitializer.class));
          // (.. 3) acquires ApplicationContextInitializer, and to set the Initializers setListeners ((Collection) getSpringFactoriesInstances (ApplicationListener.class ));
          // (4) concluded the main class this.mainApplicationClass deduceMainApplicationClass = (); }

(1). Inferential type

		this.webApplicationType = WebApplicationType.deduceFromClasspath();

  

	static WebApplicationType deduceFromClasspath() {
          //springframework.web.reactive.DispatcherHandler class
          // But org.springframework.web.servlet.DispatcherServlet and org.glassfish.jersey.servlet.ServletContainer class does not exist
          // return type WebApplicationType.REACTIVE
		if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null)
				&& !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null)
				&& !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)) {
			return WebApplicationType.REACTIVE;
		}
          //不存在javax.servlet.Servlet和org.springframework.web.context.ConfigurableWebApplicationContext

          // returns WebApplicationType.NONE

		for (String className : SERVLET_INDICATOR_CLASSES) {
			if (!ClassUtils.isPresent(className, null)) {
				return WebApplicationType.NONE;
			}
		}
		return WebApplicationType.SERVLET;
	}

  

static WebApplicationType deduceFromClasspath() {
  //springframework.web.reactive.DispatcherHandler class 
  // but org.springframework.web.servlet.DispatcherServlet class does not exist and org.glassfish.jersey.servlet.ServletContainer
  // return WebApplicationType.REACTIVE type
  if (ClassUtils.isPresent (WEBFLUX_INDICATOR_CLASS, null)
    && !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null) 
    && !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null))
  {
    return WebApplicationType.REACTIVE;
  }
  //不存在javax.servlet.Servlet和org.springframework.web.context.ConfigurableWebApplicationContext
  //返回WebApplicationType.NONE
  for (String className : SERVLET_INDICATOR_CLASSES)
  {
    if (!ClassUtils.isPresent(className, null))
    {
      return WebApplicationType.NONE; 
    }
  }
  //否则返回WebApplicationType.SERVLET
  return WebApplicationType.SERVLET;
} 

(2) disposed Initializers

		setInitializers((Collection) getSpringFactoriesInstances(
				ApplicationContextInitializer.class));

 

	private <T> Collection<T> getSpringFactoriesInstances(Class<T> type) {
		return getSpringFactoriesInstances(type, new Class<?>[] {});
	}
	private <T> Collection<T> getSpringFactoriesInstances(Class<T> type,
			Class<?>[] parameterTypes, Object... args) {
		ClassLoader classLoader = getClassLoader();
		// Use names and ensure unique to protect against duplicates
		Set<String> names = new LinkedHashSet<>(
				SpringFactoriesLoader.loadFactoryNames(type, classLoader));
		List<T> instances = createSpringFactoriesInstances(type, parameterTypes,
				classLoader, args, names);
		AnnotationAwareOrderComparator.sort(instances);
		return instances;
	}

  

setInitializers ((Collection) getSpringFactoriesInstances (// (2) Initial ApplicationContextInitializer.
ApplicationContextInitializer.class));
setListeners ((Collection) getSpringFactoriesInstances (ApplicationListener.class)); // (. 3) determines the ApplicationListener.
this.mainApplicationClass deduceMainApplicationClass = (); // (4) determining mainApplicationClass
(. 1) to infer the type of application is determined based on whether there classPath.
this.webApplicationType WebApplicationType.deduceFromClasspath = ();
. (2) determining ApplicationContextInitializer, determined object focusing the classpath read META- INF / spring.factories, and cached
setInitializers ((Collection) getSpringFactoriesInstances (
ApplicationContextInitializer.class));
. (. 3) determines the ApplicationListener, object determined, focus is META-INF / spring.factories the CLASSPATH read, and the cache
setListeners ((Collection) getSpringFactoriesInstances (ApplicationListener.class));
. (. 4) determines mainApplicationClass, focus on new RuntimeException () getStackTrace () can be acquired stack.
this.mainApplicationClass deduceMainApplicationClass = ();

Guess you like

Origin www.cnblogs.com/itivy/p/11665758.html