最近,使用Spring Cloud 2020.0.0-SNAPSHOT整合SpringBoot2.4.0、MyBatis Plus3.4.1搭建微服务,JDK版本是13.0.2。
启动微服务应用时,出现一系列异常,重点罗列如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.0)
_ _ |_ _ _|_. ___ _ | _
| | |\/|_)(_| | |_\ |_)||_|_\
/ |
3.4.1
ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'xxxController': Unsatisfied dependency expressed through field 'xxxFeignService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.edee.xxx.xxx.feign.xxxFeignService': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?
看到最后一句“Did you forget to include spring-cloud-starter-loadbalancer?”,立马想到服务依赖是否缺少?于是,尝试添加如下依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
重新启动服务,上面的异常不见了。但又出现了新的:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-12-16 11:08:07.394 ERROR 32748 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'growthChangeHistoryController': Unsatisfied dependency expressed through field 'growthChangeHistoryService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'growthChangeHistoryService': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.edee.xxx.xxx.dao.GrowthChangeHistoryDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
这个很让人头疼,百思不得其解!
怀着绝望的心情查阅网上各种资料,说法也是五花八门,一一尝试,均不奏效。
但有些文章提到了版本兼容问题,比如就有文章专门指出SpringBoot虽然已经发布了2.4.0版本,但是,不建议大家马上升级,因为版本兼容问题较多。
接下来尝试把SpringBoot降级为2.1.13.RELEASE,重启,又异常:
Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet: file ;
nested exception is java.lang.IllegalArgumentException: Unsupported class file major version 57
根据此异常,查到关键一篇博客:https://www.cnblogs.com/INnoVationv2/p/12349571.html。里面提到:jdk13暂时不被支持,降级至jdk11,可正常运行。
无奈,只能大换血,将JDK降为11.0.9。重启,依然异常:
"C:\Program Files\Java\jdk-11.0.9\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2.4\lib\idea_rt.jar=53833:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2.4\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Administrator\AppData\Local\Temp\classpath336559354.jar com.edee.xxx.ware.xxxApplication
错误: 找不到或无法加载主类 com.edee.xxx.ware.xxxApplication
原因: java.lang.ClassNotFoundException: com.edee.xxx.ware.xxxApplication
几近崩溃下,又尝试降低Spring Cloud版本,由2020.0.0-SNAPSHOT降为Greenwich.SR6。这次,终于成功了!
最终版本组合:
Spring Cloud:Greenwich.SR6;
SpringBoot:2.1.13.RELEASE;
MyBatis Plus:3.4.1;
JDK:11.0.9。
但愿该组合能够稳如泰山啊!