You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
packageorg.springframework.boot;
importorg.springframework.util.ClassUtils;
/** * An enumeration of possible types of web application. * * @author Andy Wilkinson * @author Brian Clozel * @since 2.0.0 */publicenumWebApplicationType {
/** * The application should not run as a web application and should not start an * embedded web server. */NONE,
/** * The application should run as a servlet-based web application and should start an * embedded servlet web server. */SERVLET,
/** * The application should run as a reactive web application and should start an * embedded reactive web server. */REACTIVE;
privatestaticfinalString[] SERVLET_INDICATOR_CLASSES = { "javax.servlet.Servlet",
"org.springframework.web.context.ConfigurableWebApplicationContext" };
privatestaticfinalStringWEBMVC_INDICATOR_CLASS = "org.springframework." + "web.servlet.DispatcherServlet";
privatestaticfinalStringWEBFLUX_INDICATOR_CLASS = "org." + "springframework.web.reactive.DispatcherHandler";
privatestaticfinalStringJERSEY_INDICATOR_CLASS = "org.glassfish.jersey.servlet.ServletContainer";
privatestaticfinalStringSERVLET_APPLICATION_CONTEXT_CLASS = "org.springframework.web.context.WebApplicationContext";
privatestaticfinalStringREACTIVE_APPLICATION_CONTEXT_CLASS = "org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext";
staticWebApplicationTypededuceFromClasspath() {
if (ClassUtils.isPresent(WEBFLUX_INDICATOR_CLASS, null) && !ClassUtils.isPresent(WEBMVC_INDICATOR_CLASS, null)
&& !ClassUtils.isPresent(JERSEY_INDICATOR_CLASS, null)) {
returnWebApplicationType.REACTIVE;
}
for (StringclassName : SERVLET_INDICATOR_CLASSES) {
if (!ClassUtils.isPresent(className, null)) {
returnWebApplicationType.NONE;
}
}
returnWebApplicationType.SERVLET;
}
staticWebApplicationTypededuceFromApplicationContext(Class<?> applicationContextClass) {
if (isAssignable(SERVLET_APPLICATION_CONTEXT_CLASS, applicationContextClass)) {
returnWebApplicationType.SERVLET;
}
if (isAssignable(REACTIVE_APPLICATION_CONTEXT_CLASS, applicationContextClass)) {
returnWebApplicationType.REACTIVE;
}
returnWebApplicationType.NONE;
}
privatestaticbooleanisAssignable(Stringtarget, Class<?> type) {
try {
returnClassUtils.resolveClassName(target, null).isAssignableFrom(type);
}
catch (Throwableex) {
returnfalse;
}
}
}
SpringBoot应用的类型
SpringBoot的应用有三种类型:
然后为了方便,我把相关的这个类的代码都贴出来:
这是选自2.X版本的。上面的代码很简单易读,具体来说就是:
1、如果应用程序中存在
org.springframework.web.reactive.DispatcherHandler
这个类,则表示这是一个响应式web应用,项目在启动时,需要去加载启动内嵌的响应式web服务器。2、如果应用程序中既不存在
javax.servlet.Servlet
类,也不存在org.springframework.web.context.ConfigurableWebApplicationContext
这个类,则表示当前应用不是一个web应用,启动时无需加载启动内嵌的web服务器。3、除上述两种情况外,则表示当前应用是一个servlet的web应用,启动时需要加载启动内嵌的servlet的web服务器(比如Tomcat)。
The text was updated successfully, but these errors were encountered: