Skip to content

Instantly share code, notes, and snippets.

@codeman688
Last active June 30, 2019 07:18
Show Gist options
  • Save codeman688/1cb6efeabd0c881994c9f979779c3ded to your computer and use it in GitHub Desktop.
Save codeman688/1cb6efeabd0c881994c9f979779c3ded to your computer and use it in GitHub Desktop.

Types of SpringBoot application

There are three types of SpringBoot applications:

  • none
  • Servlet-based web application
  • Reactive web application.

Then for the convenience, I posted the code of the relevant class:

package org.springframework.boot;

import org.springframework.util.ClassUtils;

/**
 * An enumeration of possible types of web application.
 *
 * @author Andy Wilkinson
 * @author Brian Clozel
 * @since 2.0.0
 */
public enum WebApplicationType {

	/**
	 * 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;

	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;
	}

	static WebApplicationType deduceFromApplicationContext(Class<?> applicationContextClass) {
		if (isAssignable(SERVLET_APPLICATION_CONTEXT_CLASS, applicationContextClass)) {
			return WebApplicationType.SERVLET;
		}
		if (isAssignable(REACTIVE_APPLICATION_CONTEXT_CLASS, applicationContextClass)) {
			return WebApplicationType.REACTIVE;
		}
		return WebApplicationType.NONE;
	}

	private static boolean isAssignable(String target, Class<?> type) {
		try {
			return ClassUtils.resolveClassName(target, null).isAssignableFrom(type);
		}
		catch (Throwable ex) {
			return false;
		}
	}

}

The code is selected from the 2.X version. The above code is very simple to read, specifically:

  1. If the class org.springframework.web.reactive.DispatcherHandler exists in the classpath, it means that this is a reactive web application. When the project starts, SpringBoot will load a embedded reactive web server.

  2. If there is neither the javax.servlet.Servlet class nor the org.springframework.web.context.ConfigurableWebApplicationContext class in classpath, then the current application is not a web application, and there is no need to load a embedded web server at startup.

  3. Except the above two cases, it indicates that the current application is a servlet-based web application, and SpringBoot will load a embedded reactive web server at startup.

Read More https://github.com/codeman-springboot/Blogs/wiki/Types-of-SpringBoot-application

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment