Preparing for Java Interview?

My books Grokking the Java Interview and Grokking the Spring Boot Interview can help

Download a Free Sample PDF

Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

Hello and welcome to the blog post. In this post, we are about to take a look at how to fix the ‘unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean’ in the spring boot application. Let’s understand how to fix this error. But before we dig deep into this issue. Let’s first have a look at when this error appears. 


Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

The error appears with the following stack trace.

Exception in thread “main” org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:140)

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:124)

at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:658)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:355)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:920)

at org.springframework.boot.SpringApplication.run(SpringApplication.java:909)

at Application.main(Application.java:17)

Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:190)

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:163)

at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137)

… 7 more 


Now, let's see some code to understand when this error comes and how to fix it:


SpringBootPracticeApplication.java


package com.practice.springboot;



import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootConfiguration;



@SpringBootConfiguration

public class SpringBootPracticeApplication {



   public static void main(String[] args) {

      SpringApplication.run(SpringBootPracticeApplication.class, args);

   }



}



pom.xml


<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>

   <parent>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-parent</artifactId>

      <version>3.0.4</version>

      <relativePath/> <!-- lookup parent from repository -->

   </parent>

   <groupId>com.practice</groupId>

   <artifactId>spring-boot</artifactId>

   <version>0.0.1-SNAPSHOT</version>

   <name>Spring Boot Practice</name>

   <description>Spring Boot Practice Project</description>

   <properties>

      <java.version>17</java.version>

   </properties>

   <dependencies>

      <dependency>

         <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-web</artifactId>

      </dependency>



	 <dependency>

       <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-test</artifactId>

         <scope>test</scope>

	</dependency>

   </dependencies>

</project>


As you can see from the above program, we have a very simple spring boot project that fails to run as intended. There are a couple of possible solutions to this program. These approaches are discussed below.


How to fix this error?

First of all you need to ensure that your main class has the @SpringBootApplication annotation.

The @SpringBootApplication annotation is comparable to the @EnableAutoConfiguration, @ComponentScan, and @Configuration annotations with their default properties, i.e., allow adding new beans to the context or importing more configuration classes.


SpringBootPracticeApplication.java -- updated

package com.practice.springboot;



import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;



@SpringBootApplication

public class SpringBootPracticeApplication {



   public static void main(String[] args) {

      SpringApplication.run(SpringBootPracticeApplication.class, args);

   }



}


If you have followed the above step or your starter file already contains @SpringBootApplication, you need to make sure your pom.xml file also includes the spring-boot-starter-web or spring-boot-starter-tomcat dependencies, as demonstrated in the example below.
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>

   <parent>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-parent</artifactId>

      <version>3.0.4</version>

      <relativePath/> <!-- lookup parent from repository -->

   </parent>

   <groupId>com.practice</groupId>

   <artifactId>spring-boot</artifactId>

   <version>0.0.1-SNAPSHOT</version>

   <name>Spring Boot Practice</name>

   <description>Spring Boot Practice Project</description>

   <properties>

      <java.version>17</java.version>

   </properties>

   <dependencies>

      <dependency>

         <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-web</artifactId>

      </dependency>



	 <dependency>

       <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-test</artifactId>

         <scope>test</scope>

	</dependency>



<!-- Add spring-boot-starter-web or spring-boot-starter-tomcat -->



<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-tomcat</artifactId>

</dependency>



<!-- Add spring-boot-starter-web or spring-boot-starter-tomcat -->



   </dependencies>

</project>


After following the above two approaches, the error finally disappeared and the spring application start successfully as shown from the console.

 .   ____          _            __ _ _

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )

  '  |____| .__|_| |_|_| |_\__, | / / / /

 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.0.4)


2023-03-09T09:46:05.540+05:00  INFO 40009 --- [           main] c.p.s.SpringBootPracticeApplication      : Starting SpringBootPracticeApplication using Java 19.0.1 with PID 40009 (/home/muhammad/IdeaProjects/spring-boot/target/classes started by muhammad in /home/muhammad/IdeaProjects/spring-boot)

2023-03-09T09:46:05.545+05:00  INFO 40009 --- [           main] c.p.s.SpringBootPracticeApplication      : No active profile set, falling back to 1 default profile: "default"

2023-03-09T09:46:06.501+05:00  INFO 40009 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)

2023-03-09T09:46:06.509+05:00  INFO 40009 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]

2023-03-09T09:46:06.510+05:00  INFO 40009 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.5]

2023-03-09T09:46:06.585+05:00  INFO 40009 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext

2023-03-09T09:46:06.585+05:00  INFO 40009 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 972 ms

2023-03-09T09:46:06.964+05:00  INFO 40009 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''

2023-03-09T09:46:06.972+05:00  INFO 40009 --- [           main] c.p.s.SpringBootPracticeApplication      : Started SpringBootPracticeApplication in 1.927 seconds (process running for 2.661)


Conclusion

That's all about how to fix "Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean" error in Spring Boot. . The main subject of this post is the Spring Boot problem Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean. 

We have explored two methods to address this issue. The first step is to choose the appropriate Spring annotation, and the second is to add the missing dependency to your build.gradle or pom.xml file. I hope the information in this post has helped you to correct the issue. For more such helpful articles keep following us.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.