title: Properly terminate Spring Boot and Tomcat if initialization of AppContext fails
body: <p>When I start Spring Boot application on embedded Tomcat and something fails during startup (for instance there is no connection to DB or Liquibase update fails or circular dependency found, etc), Tomcat continues listening on 8080 port.</p>
  
  <p>If I send HTTP request to some endpoint, an application returns 404 NOT FOUND.</p>
  
  <ol>
  <li>Is this expected behavior (should Tomcat continue listening)?</li>
  <li>How to stop Spring Boot and Tomcat when initialization of the application context fails?</li>
  </ol>
  
  <p>Here EXAMPLE of the log:</p>
  
  <pre><code>org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatEmbeddedServletContainerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/EmbeddedServletContainerAutoConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverProperties' defined in class path resource [org/springframework/boot/autoconfigure/web/ServerPropertiesAutoConfiguration.class]: Initialization of bean failed; nested exception is javax.validation.ValidationException: Unable to instantiate Configuration.
      at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:536)
      at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
      at com.test.Application.main(Application.java:33)
  </code></pre>
  
  <p>After this exception Tomcat still available on the specified port.</p>
  
  <p><strong>UPD</strong></p>
  
  <p>application.properties:</p>
  
  <pre><code>spring.profiles.default=production
  spring.application.name=test
  
  spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
  spring.datasource.username=test
  spring.datasource.password=test123
  spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
  
  spring.jpa.hibernate.ddl-auto=none
  
  liquibase.change-log=classpath:/liquibase/changelog.xml
  </code></pre>
  
  <p>dependencies:</p>
  
  <pre><code>&lt;properties&gt;
      &lt;spring.boot.version&gt;1.5.9.RELEASE&lt;/spring.boot.version&gt;
  
      &lt;liquibase.version&gt;3.5.3&lt;/liquibase.version&gt;
      &lt;postgresql.version&gt;9.4-1201-jdbc41&lt;/postgresql.version&gt;
      &lt;hsqldb.version&gt;2.4.0&lt;/hsqldb.version&gt;
  
      &lt;powermock.version&gt;1.7.3&lt;/powermock.version&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;version&gt;${spring.boot.version}&lt;/version&gt;
          &lt;exclusions&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
                  &lt;artifactId&gt;tomcat-embed-el&lt;/artifactId&gt;
              &lt;/exclusion&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
                  &lt;artifactId&gt;tomcat-annotations-api&lt;/artifactId&gt;
              &lt;/exclusion&gt;
          &lt;/exclusions&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;version&gt;${spring.boot.version}&lt;/version&gt;
          &lt;exclusions&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
                  &lt;artifactId&gt;tomcat-juli&lt;/artifactId&gt;
              &lt;/exclusion&gt;
          &lt;/exclusions&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
          &lt;version&gt;${spring.boot.version}&lt;/version&gt;
          &lt;exclusions&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;org.codehaus.groovy&lt;/groupId&gt;
                  &lt;artifactId&gt;groovy&lt;/artifactId&gt;
              &lt;/exclusion&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;nz.net.ultraq.thymeleaf&lt;/groupId&gt;
                  &lt;artifactId&gt;thymeleaf-layout-dialect&lt;/artifactId&gt;
              &lt;/exclusion&gt;
          &lt;/exclusions&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-autoconfigure&lt;/artifactId&gt;
          &lt;version&gt;${spring.boot.version}&lt;/version&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
          &lt;version&gt;${spring.boot.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;version&gt;${spring.boot.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
          &lt;version&gt;4.3.14.RELEASE&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.postgresql&lt;/groupId&gt;
          &lt;artifactId&gt;postgresql&lt;/artifactId&gt;
          &lt;version&gt;${postgresql.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.liquibase&lt;/groupId&gt;
          &lt;artifactId&gt;liquibase-core&lt;/artifactId&gt;
          &lt;version&gt;${liquibase.version}&lt;/version&gt;
          &lt;scope&gt;compile&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.hsqldb&lt;/groupId&gt;
          &lt;artifactId&gt;hsqldb&lt;/artifactId&gt;
          &lt;version&gt;${hsqldb.version}&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.powermock&lt;/groupId&gt;
          &lt;artifactId&gt;powermock-module-junit4&lt;/artifactId&gt;
          &lt;version&gt;${powermock.version}&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.powermock&lt;/groupId&gt;
          &lt;artifactId&gt;powermock-api-mockito&lt;/artifactId&gt;
          &lt;version&gt;${powermock.version}&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
          &lt;artifactId&gt;hamcrest-library&lt;/artifactId&gt;
          &lt;version&gt;${hamcrest.version}&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
          &lt;artifactId&gt;hamcrest-core&lt;/artifactId&gt;
          &lt;version&gt;${hamcrest.version}&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;com.google.code.simple-spring-memcached&lt;/groupId&gt;
          &lt;artifactId&gt;spring-cache&lt;/artifactId&gt;
          &lt;version&gt;3.6.1&lt;/version&gt;
          &lt;exclusions&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
                  &lt;artifactId&gt;hamcrest-core&lt;/artifactId&gt;
              &lt;/exclusion&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;commons-collections&lt;/groupId&gt;
                  &lt;artifactId&gt;commons-collections&lt;/artifactId&gt;
              &lt;/exclusion&gt;
              &lt;exclusion&gt;
                  &lt;groupId&gt;junit&lt;/groupId&gt;
                  &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;/exclusion&gt;
          &lt;/exclusions&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.codehaus.janino&lt;/groupId&gt;
          &lt;artifactId&gt;janino&lt;/artifactId&gt;
          &lt;version&gt;3.0.8&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;
          &lt;artifactId&gt;logback-classic&lt;/artifactId&gt;
          &lt;version&gt;1.2.3&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;
          &lt;artifactId&gt;logback-core&lt;/artifactId&gt;
          &lt;version&gt;1.2.3&lt;/version&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  
  &lt;dependencyManagement&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-dependencies&lt;/artifactId&gt;
              &lt;version&gt;${spring.boot.version}&lt;/version&gt;
              &lt;type&gt;pom&lt;/type&gt;
              &lt;scope&gt;import&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  &lt;/dependencyManagement&gt;
  </code></pre>
  
  <p><strong>UPD 2</strong></p>
  
  <p>Application.java:</p>
  
  <pre><code>@SpringBootApplication
  @EnableJpaRepositories(basePackages = "com.test")
  @EnableCaching
  @EnableScheduling
  public class Application extends SpringBootServletInitializer {
  
      public Application() {
      }
  
      public static void main(final String[] args) {
          SpringApplication.run(Application.class, args);
      }
  }
  </code></pre>