初学Spring Cloud踩得坑之Caused by: org.springframework.context.ApplicationContextException

初学Spring Cloud踩得坑之Caused by: org.springframework.context.ApplicationContextException
强烈推介IDEA2020.2破解激活,IntelliJ IDEA 注册码,2020.2 IDEA 激活码

初学Spring Cloud踩得坑之

Caused by: org.springframework.context.ApplicationContextException:
Unable to start ServletWebServerApplicationContext due to missing
ServletWebServerFactory bean.

Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

具体运行错误代码如下:

D:\java\jdk1.8.0_201\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:C:\JetBrains\idea\Intellij IDEA\IntelliJ IDEA 2018.1.4\lib\idea_rt.jar=61907:C:\JetBrains\idea\Intellij IDEA\IntelliJ IDEA 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath D:\java\jdk1.8.0_201\jre\lib\charsets.jar;D:\java\jdk1.8.0_201\jre\lib\deploy.jar;D:\java\jdk1.8.0_201\jre\lib\ext\access-bridge-64.jar;D:\java\jdk1.8.0_201\jre\lib\ext\cldrdata.jar;D:\java\jdk1.8.0_201\jre\lib\ext\dnsns.jar;D:\java\jdk1.8.0_201\jre\lib\ext\jaccess.jar;D:\java\jdk1.8.0_201\jre\lib\ext\jfxrt.jar;D:\java\jdk1.8.0_201\jre\lib\ext\localedata.jar;D:\java\jdk1.8.0_201\jre\lib\ext\nashorn.jar;D:\java\jdk1.8.0_201\jre\lib\ext\sunec.jar;D:\java\jdk1.8.0_201\jre\lib\ext\sunjce_provider.jar;D:\java\jdk1.8.0_201\jre\lib\ext\sunmscapi.jar;D:\java\jdk1.8.0_201\jre\lib\ext\sunpkcs11.jar;D:\java\jdk1.8.0_201\jre\lib\ext\zipfs.jar;D:\java\jdk1.8.0_201\jre\lib\javaws.jar;D:\java\jdk1.8.0_201\jre\lib\jce.jar;D:\java\jdk1.8.0_201\jre\lib\jfr.jar;D:\java\jdk1.8.0_201\jre\lib\jfxswt.jar;D:\java\jdk1.8.0_201\jre\lib\jsse.jar;D:\java\jdk1.8.0_201\jre\lib\management-agent.jar;D:\java\jdk1.8.0_201\jre\lib\plugin.jar;D:\java\jdk1.8.0_201\jre\lib\resources.jar;D:\java\jdk1.8.0_201\jre\lib\rt.jar;C:\User\workspace_idea\leyou-shop\cloud-demo\user-serivce\target\classes;D:\MavenRepositories\org\springframework\boot\spring-boot-starter-web\2.0.4.RELEASE\spring-boot-starter-web-2.0.4.RELEASE.jar;D:\MavenRepositories\org\springframework\boot\spring-boot-starter\2.0.4.RELEASE\spring-boot-starter-2.0.4.RELEASE.jar;D:\MavenRepositories\org\springframework\boot\spring-boot\2.0.4.RELEASE\spring-boot-2.0.4.RELEASE.jar;D:\MavenRepositories\org\springframework\boot\spring-boot-autoconfigure\2.0.4.RELEASE\spring-boot-autoconfigure-2.0.4.RELEASE.jar;D:\MavenRepositories\org\springframework\boot\spring-boot-starter-logging\2.0.4.RELEASE\spring-boot-starter-logging-2.0.4.RELEASE.jar;D:\MavenRepositories\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\MavenRepositories\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\MavenRepositories\org\apache\logging\log4j\log4j-to-slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar;D:\MavenRepositories\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar;D:\MavenRepositories\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;D:\MavenRepositories\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\MavenRepositories\org\springframework\spring-core\5.0.8.RELEASE\spring-core-5.0.8.RELEASE.jar;D:\MavenRepositories\org\springframework\spring-jcl\5.0.8.RELEASE\spring-jcl-5.0.8.RELEASE.jar;D:\MavenRepositories\org\yaml\snakeyaml\1.19\snakeyaml-1.19.jar;D:\MavenRepositories\org\springframework\boot\spring-boot-starter-json\2.0.4.RELEASE\spring-boot-starter-json-2.0.4.RELEASE.jar;D:\MavenRepositories\com\fasterxml\jackson\core\jackson-databind\2.9.6\jackson-databind-2.9.6.jar;D:\MavenRepositories\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\MavenRepositories\com\fasterxml\jackson\core\jackson-core\2.9.6\jackson-core-2.9.6.jar;D:\MavenRepositories\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.6\jackson-datatype-jdk8-2.9.6.jar;D:\MavenRepositories\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.6\jackson-datatype-jsr310-2.9.6.jar;D:\MavenRepositories\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.6\jackson-module-parameter-names-2.9.6.jar;D:\MavenRepositories\org\springframework\boot\spring-boot-starter-tomcat\2.0.4.RELEASE\spring-boot-starter-tomcat-2.0.4.RELEASE.jar;D:\MavenRepositories\org\apache\tomcat\embed\tomcat-embed-core\8.5.32\tomcat-embed-core-8.5.32.jar;D:\MavenRepositories\org\apache\tomcat\embed\tomcat-embed-el\8.5.32\tomcat-embed-el-8.5.32.jar;D:\MavenRepositories\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.32\tomcat-embed-websocket-8.5.32.jar;D:\MavenRepositories\org\hibernate\validator\hibernate-validator\6.0.11.Final\hibernate-validator-6.0.11.Final.jar;D:\MavenRepositories\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\MavenRepositories\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;D:\MavenRepositories\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;D:\MavenRepositories\org\springframework\spring-web\5.0.8.RELEASE\spring-web-5.0.8.RELEASE.jar;D:\MavenRepositories\org\springframework\spring-beans\5.0.8.RELEASE\spring-beans-5.0.8.RELEASE.jar;D:\MavenRepositories\org\springframework\spring-webmvc\5.0.8.RELEASE\spring-webmvc-5.0.8.RELEASE.jar;D:\MavenRepositories\org\springframework\spring-aop\5.0.8.RELEASE\spring-aop-5.0.8.RELEASE.jar;D:\MavenRepositories\org\springframework\spring-context\5.0.8.RELEASE\spring-context-5.0.8.RELEASE.jar;D:\MavenRepositories\org\springframework\spring-expression\5.0.8.RELEASE\spring-expression-5.0.8.RELEASE.jar;D:\MavenRepositories\mysql\mysql-connector-java\5.1.32\mysql-connector-java-5.1.32.jar;D:\MavenRepositories\tk\mybatis\mapper-spring-boot-starter\2.0.3\mapper-spring-boot-starter-2.0.3.jar;D:\MavenRepositories\org\springframework\boot\spring-boot-starter-jdbc\2.0.4.RELEASE\spring-boot-starter-jdbc-2.0.4.RELEASE.jar;D:\MavenRepositories\com\zaxxer\HikariCP\2.7.9\HikariCP-2.7.9.jar;D:\MavenRepositories\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\MavenRepositories\org\springframework\spring-jdbc\5.0.8.RELEASE\spring-jdbc-5.0.8.RELEASE.jar;D:\MavenRepositories\org\springframework\spring-tx\5.0.8.RELEASE\spring-tx-5.0.8.RELEASE.jar;D:\MavenRepositories\org\mybatis\mybatis\3.4.6\mybatis-3.4.6.jar;D:\MavenRepositories\org\mybatis\mybatis-spring\1.3.2\mybatis-spring-1.3.2.jar;D:\MavenRepositories\tk\mybatis\mapper-core\1.0.4\mapper-core-1.0.4.jar;D:\MavenRepositories\javax\persistence\persistence-api\1.0\persistence-api-1.0.jar;D:\MavenRepositories\tk\mybatis\mapper-base\1.0.4\mapper-base-1.0.4.jar;D:\MavenRepositories\tk\mybatis\mapper-weekend\1.1.3\mapper-weekend-1.1.3.jar;D:\MavenRepositories\tk\mybatis\mapper-spring\1.0.4\mapper-spring-1.0.4.jar;D:\MavenRepositories\tk\mybatis\mapper-extra\1.0.4\mapper-extra-1.0.4.jar;D:\MavenRepositories\tk\mybatis\mapper-spring-boot-autoconfigure\2.0.3\mapper-spring-boot-autoconfigure-2.0.3.jar;D:\MavenRepositories\org\projectlombok\lombok\1.16.22\lombok-1.16.22.jar cn.itcast.UserApplication

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.4.RELEASE)

2019-09-14 11:15:48.564  INFO 9696 --- [           main] cn.itcast.UserApplication                : Starting UserApplication on DESKTOP-12MVDLN with PID 9696 (C:\User\workspace_idea\leyou-shop\cloud-demo\user-serivce\target\classes started by 凌枫lqg in C:\User\workspace_idea\leyou-shop\cloud-demo)
2019-09-14 11:15:48.579  INFO 9696 --- [           main] cn.itcast.UserApplication                : No active profile set, falling back to default profiles: default
2019-09-14 11:15:48.688  INFO 9696 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7a69b07: startup date [Sat Sep 14 11:15:48 CST 2019]; root of context hierarchy
2019-09-14 11:15:49.133  WARN 9696 --- [           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.
2019-09-14 11:15:49.649 ERROR 9696 --- [           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:155) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:330) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at cn.itcast.UserApplication.main(UserApplication.java:13) [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:204) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	... 8 common frames omitted


Process finished with exit code 1

出错原因:以前学Spring Boot习惯把启动类的 SpringApplication.run(UserApplication.class,args);中的写成SpringApplication.class,实际是要学成Spring Boot启动类的类名,犯了这么低级的错,找了半天才找出来,改了之后,demo就能照常运行。书写此文,以示警戒,也可以帮助到那些和我一样粗心的人。

本文来源凌枫zero,由架构君转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/8010

发表评论