19

I have a spring-boot web application, but I don't want to start it in embedded Tomcat/Jetty. What is the correct way to disable embedded container?

If I do smth like:

        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
           <exclusion>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-starter-tomcat</artifactId>
           </exclusion>
        </exclusions>
    </dependency>

I keep getting

org.springframework.context.ApplicationContextException: Unable to start embedded container; 
andrew.z
  • 1,039
  • 3
  • 15
  • 24

1 Answers1

24

Since you are using Maven (instead of Gradle) check out this guide and this part of the official documentation.

The basic steps are:

Make the embedded servlet container a provided dependency (therefore removing it from the produced war)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

Add an application initializer like:

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

public class WebInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

}

That class is needed in order to be able to bootstrap the Spring application since there is no web.xml used.

geoand
  • 60,071
  • 24
  • 172
  • 190
  • 1
    The `return application.sources(Application.class)` probably should be `return application.sources(WebInitializer.class)` (unless there is another Application class). A good reference is http://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html . That doc also puts `@SpringBootApplication` on the application class. – djb Aug 04 '15 at 16:06
  • Thanks for the comment! The doc refers to the latest version. When I added the answer about a year ago I am fairly sure there was no `@SpringBootApplication` annotation. Also the answer made the assumption that there exists a main `Application` class for bootstrapping Spring Boot – geoand Aug 04 '15 at 17:28