SpringBoot启动报错Error creating bean with name ‘xxxx‘ defined in class path resource……

2年前 (2022) 程序员胖胖胖虎阿
161 0 0

项目场景:

在SpringBoot项目启动时的报错,切身体验,记录解决报错全过程,希望能给看到的人一些解决方案和思路。在遇到诸如此类的项目启动报错时,不要无从下手,仔细去从报错日志中寻找有效的信息。要坚定信念:事出有妖必有因。


问题描述:

既然是问题,那就直接贴上报错日志:

Error creating bean with name 'mapperScannerConfigurer' defined in class path resource [com/gwm/tsp/boot/dao/config/datasource/DataSourceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.mybatis.spring.mapper.MapperScannerConfigurer]: Factory method 'mapperScannerConfigurer' threw exception; nested exception is com.gwm.tsp.boot.common.exception.SysException……

Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'PlatUserInfoResponse'.  Cause: java.lang.ClassNotFoundException: Cannot find class: PlatUserInfoResponse……

Caused by: java.lang.ClassNotFoundException: Cannot find class: PlatUserInfoResponse
tat org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:200)
tat org.apache.ibatis.io.ClassLoaderWrapper.classForName(ClassLoaderWrapper.java:89)
tat org.apache.ibatis.io.Resources.classForName(Resources.java:261)
tat org.apache.ibatis.type.TypeAliasRegistry.resolveAlias(TypeAliasRegistry.java:116)
t... 46 common frames omitted

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

Process finished with exit code 1


原因分析:

报错日志很多,我们在分析问题时要学会取舍性的去看对我们解决问题有用的日志信息。从上面我截取的报错日志中可以看出BeanInstantiationException和TypeException这两种报错。同时又有Error creating bean with name 'mapperScannerConfigurer'这种显而易见的创建某一实体类报错信息。其实最重要的报错信息是:org.apache.ibatis.type.TypeException: Could not resolve type alias 'PlatUserInfoResponse'.可以判定是mybatis使用以及返回类型报错,经过排查是mapper.xml文件中PlatUserInfoResponse返回值路径有问题,至此问题得以解决。


解决方案:

遇到诸如此类的报错信息无非从以下几点去寻找方向:

1、jar包冲突使得SpringBoot重复引用某一包的不同版本,导致createBean失败;

2、某一返回实体,路径不存在,导致表面上报createBean失败错误;

3、扫描包的时候扫描不到(如果数据库连接信息,在初始化包扫描时是以配置文件形式配置的,比如nacos配置。检查配置文件包路径配置是否跟代码里的包路径一致,同时避免重复扫描现象)

4、SpringBoot 循环依赖问题也可以从本案例中得到解决方案。

相关文章

暂无评论

暂无评论...