Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot without the web server

People also ask

Can we run Spring Boot without Tomcat?

You can use Spring Boot without embedded Tomcat web server if you don't need it. Just exclude embedded Tomcat from Spring Boot Web Starter (spring-boot-starter-web).

Can you disable the default web server in the Spring Boot application?

Actually, Spring boot by default comes up with the embedded server once we add “spring-boot-starter-web” dependency. But, Spring boot gives us the flexibility to use tomcat or not. If we do not want we can exclude this default server. Default, Spring boot comes with 3 types of embed servers Tomcat, Jetty and undertow.

Can we run Spring Boot application without @SpringBootApplication?

Uses. It's not mandatory to put @SpringBootApplication to create a Spring Boot application, you can still use @Configuration and @EnableAutoConfiguration individually as shown in the example given in the next point.

Does Spring Boot need a web server?

One of the most popular uses is as a web server, using one of the many supported embedded servlet containers and template engines. However, Spring Boot has a number of uses that do not require a web server: console applications, job scheduling, batch or stream processing, serverless applications, and more.


if you want to run spring boot without a servlet container, but with one on the classpath (e.g. for tests), use the following, as described in the spring boot documentation:

@Configuration
@EnableAutoConfiguration
public class MyClass {
    public static void main(String[] args) throws JAXBException {
         SpringApplication app = new SpringApplication(MyClass.class);
         app.setWebEnvironment(false); //<<<<<<<<<
         ConfigurableApplicationContext ctx = app.run(args);
    }
}

also, I just stumbled across this property:

spring.main.web-environment=false

Spring Boot 2.x

  • Application Properties

    spring.main.web-application-type=NONE 
    # REACTIVE, SERVLET
    
  • or SpringApplicationBuilder

    @SpringBootApplication
    public class MyApplication {
    
        public static void main(String[] args) {
            new SpringApplicationBuilder(MyApplication.class)
                .web(WebApplicationType.NONE) // .REACTIVE, .SERVLET
                .run(args);
       }
    }
    

Where WebApplicationType:

  • NONE - The application should not run as a web application and should not start an embedded web server.
  • REACTIVE - The application should run as a reactive web application and should start an embedded reactive web server.
  • SERVLET - The application should run as a servlet-based web application and should start an embedded servlet web server.

You can create something like this:

@SpringBootApplication
public class Application {
  public static void main(String[] args) {
    new SpringApplicationBuilder(Application.class).web(false).run(args);
  }
}

And

@Component
public class CommandLiner implements CommandLineRunner {

  @Override
  public void run(String... args) throws Exception {
    // Put your logic here
  }

}

The dependency is still there though but not used.


Spring boot will not include embedded tomcat if you don't have Tomcat dependencies on the classpath. You can view this fact yourself at the class EmbeddedServletContainerAutoConfiguration whose source you can find here.

The meat of the code is the use of the @ConditionalOnClass annotation on the class EmbeddedTomcat


Also, for more information check out this and this guide and this part of the documentation


Use this code.

SpringApplication application = new SpringApplication(DemoApplication.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);