title: spring-boot web app fails to start : Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
body: <p>I'm trying to start my spring-boot-web app but I'm getting the following error : </p>
  
  <pre><code>2020-03-17 20:54:22.643  WARN 15640 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
  2020-03-17 20:54:22.664 ERROR 15640 --- [           main] o.s.boot.SpringApplication               : Application run failed
  </code></pre>
  
  <p>I configured the following ApplicationContext file : </p>
  
  <pre><code>@Configuration
  @EnableJpaRepositories(basePackages = {"repos"})
  public class AppConfig  {
      @Bean
      public LocalEntityManagerFactoryBean entityManagerFactory() {
          LocalEntityManagerFactoryBean factoryBean = new LocalEntityManagerFactoryBean();
          factoryBean.setPersistenceUnitName("mydb");
  
          return factoryBean;
      }
  
      @Bean
      public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(entityManagerFactory);
  
          return transactionManager;
      }
  }
  </code></pre>
  
  <p>I need those beans because in one of my services I inject the following objects :</p>
  
  <pre><code>@Autowired
  private EntityManagerFactory emf;
  
  @PersistenceContext
  private EntityManager em;
  </code></pre>
  
  <p>and my main app : </p>
  
  <pre><code>@SpringBootApplication
  public class Application  extends SpringBootServletInitializer {
  
  
      public static void main(String[]args)
      {
          //load the spring config class we configured
          SpringApplication.run(AppConfig.class);
      }
  </code></pre>
  
  <p>my dependencies in pom.xml : </p>
  
  <pre><code>  &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;2.2.5.RELEASE&lt;/version&gt;
          &lt;/dependency&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;2.2.5.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
              &lt;version&gt;5.2.4.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
              &lt;version&gt;5.2.4.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;5.4.1.Final&lt;/version&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;42.2.11&lt;/version&gt;
          &lt;/dependency&gt;
  </code></pre>
  
  <p>Solutions I tried but didnt work : </p>
  
  <p>1)add extends SpringBootServletInitializer to both Application.java and Appconfig.java</p>
  
  <p>2)Move the beans from the AppConfig.java to Application.java</p>
  
  <p>3)Tried to set the following annotation on  the application class : </p>
  
  <pre><code>@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
  </code></pre>
  
  <p>4)Adding the following dependency to the pom.xml : </p>
  
  <p>
          org.springframework.boot
          spring-boot-starter-tomcat
          2.2.5.RELEASE
      </p>