[Solved] org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicat…

First of all, the following errors are reported when starting the project: org.springframework.context.applicationcontextexception: unable to start servlet webserver ApplicationContext due to missing servlet webserverfactory bean. org.springframework.context.applicationcontextexception: unable to start servlet webserver ApplicationContext due to missing servlet webserverfactory bean

There are two solutions (I used the first one to solve the problem)

introduce web dependency

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

set web-application-type:

spring:
  main:
    web-application-type: none

Solution process:

When starting the project, we encountered the following error:

2019-07-10 00:20:14.844 ERROR [,,,] 5332 --- [           main] o.s.boot.SpringApplication               : Application run failed

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.
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:157) ~[spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:540) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at com.smartit.spcloud.server.cms.ServerCmsApplication.main(ServerCmsApplication.java:21) [classes/:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:206) ~[spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180) ~[spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:154) ~[spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE]
	... 8 common frames omitted

Disconnected from the target VM, address: '127.0.0.1:7553', transport: 'socket'

Process finished with exit code 1

in general, the port is occupied, but I didn’t see the port error log this time, so I googled, It is said that the solution is to add a section of configuration in application. YML (original link: https://stackoverflow.com/questions/50231736/applicationcontextexception-unable-to-start-servletwebserverapplicationcontext/50232382)

spring:
  main:
    web-application-type: none

after adding startup, the previous error will no longer be reported. However, after registering with Eureka, it will be automatically closed. The log is as follows:

15144 --- [           main] c.s.s.server    : Started  in 14.068 seconds (JVM running for 15.815)
2019-07-10 00:40:40.488  INFO ,,,] 15144 --- [      Thread-35] o.s.c.n.e.s.EurekaServiceRegistry        : Unregistering application with eureka with status DOWN
2019-07-10 00:40:40.488  WARN [,,,] 15144 --- [      Thread-35] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1562690440488, current=DOWN, previous=UP]
2019-07-10 00:40:40.497  INFO [,,,] 15144 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_/192.168.0.106:8888 - registration status: 204
2019-07-10 00:40:40.498  INFO [,,,] 15144 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient/192.168.0.106:8888: registering service...
2019-07-10 00:40:40.502  INFO [,,,] 15144 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_SE/192.168.0.106:8888 - registration status: 204
2019-07-10 00:40:40.821  WARN [,,,] 15144 --- [ender@10545d02}] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'eurekaRibbonClientConfiguration': Unsatisfied dependency expressed through field 'eurekaConfig'; nested exception is org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'eurekaInstanceConfigBean': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
2019-07-10 00:40:40.862  INFO [,,,] 15144 --- [      Thread-35] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} closed
2019-07-10 00:40:40.863  INFO [,,,] 15144 --- [      Thread-35] com.netflix.discovery.DiscoveryClient    : Shutting down DiscoveryClient ...
2019-07-10 00:40:43.864  INFO [,,,] 15144 --- [      Thread-35] com.netflix.discovery.DiscoveryClient    : Unregistering ...
2019-07-10 00:40:43.877  INFO [,,,] 15144 --- [      Thread-35] com.netflix.discovery.DiscoveryClient    : DiscoveryClient/192.168.0.106:8888 - deregister  status: 200
2019-07-10 00:40:43.893  INFO [,,,] 15144 --- [      Thread-35] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient
Disconnected from the target VM, address: '127.0.0.1:9892', transport: 'socket'

Process finished with exit code 0
562690440488, current=DOWN, previous=UP]
2019-07-10 00:40:40.497  INFO [] 15144 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient/192.168.0.106::8888 - registration status: 204
2019-07-10 00:40:40.498  INFO [,,,] 15144 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient/192.168.0.106:8888: registering service...
2019-07-10 00:40:40.502  INFO [,,,] 15144 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient/192.168.0.106:8888 - registration status: 204
2019-07-10 00:40:40.821  WARN [,,,] 15144 --- [ender@10545d02}] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'eurekaRibbonClientConfiguration': Unsatisfied dependency expressed through field 'eurekaConfig'; nested exception is org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'eurekaInstanceConfigBean': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)
2019-07-10 00:40:40.862  INFO [] 15144 --- [      Thread-35] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} closed
2019-07-10 00:40:40.863  INFO [] 15144 --- [      Thread-35] com.netflix.discovery.DiscoveryClient    : Shutting down DiscoveryClient ...
2019-07-10 00:40:43.864  INFO [] 15144 --- [      Thread-35] com.netflix.discovery.DiscoveryClient    : Unregistering ...
2019-07-10 00:40:43.877  INFO [] 15144 --- [      Thread-35] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_/192.168.0.106:8888 - deregister  status: 200
2019-07-10 00:40:43.893  INFO [] 15144 --- [      Thread-35] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient
Disconnected from the target VM, address: '127.0.0.1:9892', transport: 'socket'

Process finished with exit code 0

Most of the Internet users say that they do not add Web dependencies, so they add Web dependencies in pom.xml

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

start, but still register with Eureka and then close. Think about it, since this situation is caused by the application is not a web application, and web dependency has been added, then the previous configuration may mark the application as a non web application, so delete the previous configuration, and you can start to register with Eureka

Similar Posts: