先说结论,启动项目时遇到以下报错:org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
,解决方法有两个(我是使用第一种方法解决的):
-
引入web依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
-
设置web-application-type:
spring: main: web-application-type: none
解决流程:
启动项目的时候遇到了如下报错:
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
-
往常遇到这种报错,一般都是端口被占用,但是这次往上翻了翻没看到端口报错日志,于是google了一下,看到stackoverflow上有人说解决方法是在
application.yml
里添加一段配置(原文链接:)
spring: main: web-application-type: none
-
添加启动后倒是不再报刚才的错误,但是刚启动注册到eureka,又会自动关闭,日志如下:
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
网上大部分说是没有添加web依赖,于是在pom.xml中添加了web依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
-
启动,但是仍然是注册到eureka后又关闭,想了下,既然这种情况是由于应用不是web应用引起的,已经添加了web依赖,那么就可能是之前的配置将应用标记为了非web应用,于是删除之前的配置,果然可以启动注册到eureka了