1.spring启动类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
run方法调用如下方法,最终创建了一个SpringApplication对象
2.SpringApplication.java
public static ConfigurableApplicationContext run(Class<?> primarySource,
String... args) {
return run(new Class<?>[] { primarySource }, args);
}
public static ConfigurableApplicationContext run(Class<?>[] primarySources,
String[] args) {
return new SpringApplication(primarySources).run(args);
}
//new 对象调用
public SpringApplication(Class<?>... primarySources) {
this(null, primarySources);
}
//最终调用构造方法
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader; //默认为空
Assert.notNull(primarySources, "PrimarySources must not be null");
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources)); //源对象 ,可以多个,不能重复
//deduce:演绎; 推论,推断; 追溯根源;
this.webApplicationType = WebApplicationType.deduceFromClasspath();//从类路径推断当前应用类型 (NONE ,SERVLET-web,REACTIVE-响应式程序) 见3 说明
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}
3.WebApplicationType.java 枚举类,用于判断当前项目类型
public enum WebApplicationType {
/**
* The application should not run as a web application and should not start an
* embedded web server.
*/
NONE,//非web应用,不能作为web应用,不能启动一个内嵌的web服务器
/**
* The application should run as a servlet-based web application and should start an
* embedded servlet web server.
*/
SERVLET, //web应用
/**
* The application should run as a reactive web application and should start an
* embedded reactive web server.
*/
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() {
//判断是否存在类WEBFLUX_INDICATOR_CLASS 并且不存在 servlet
if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null)
//不存在 mvc相关的类
&& !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;
}