Mybatis添加依赖后产生错误:org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [mybatis/mybatis-config.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.logging.LogException: Error setting Log implementation. Cause: java.lang.reflect.InvocationTargetExceptionlog4j引发的错误
- 问题复现
- 产生原因及解决
- 深究原因:
- 附:SpringBoot运行报错:
mybatis配置添加日志后
maven中的Dependencies会有红色波浪线:log4j:log4j:unknown
查看了一下pom.xml中关于log4j的依赖
产生原因及解决log4j log4j compile
其中没有写版本号,查阅资料后发现:
因为:
log4j自1.2.17版本以后,就改变jar包了
比如:1.2.17及以前的版本的mvn依赖是这样写的:
log4j log4j 1.2.17
然而1.2.17以后的版本的mvn依赖是这样写的:
org.apache.logging.log4j log4j-core 2.13.1
于是,按第一种方法显示的声明使用1.2.17版本,问题解决。
之后想要通过修改pom中依赖的设置再复现问题发现复现不了,我猜测的原因是maven已经处理好了相关的依赖,需要maven clean操作
深究原因:我们知道SpringBoot中的依赖不用写版本号是因为其自动版本仲裁机制,我们点击项目的父项目
org.springframework.boot spring-boot-starter-parent 2.5.4
ctrl加左键点击spring-boot-starter-parent,发现这个项目还有父项目spring-boot-dependencies
org.springframework.boot spring-boot-dependencies 2.5.4
进入spring-boot-dependencies后可以看到里面声明了开发过程中常用的jar包的版本号和依赖
我们搜索log4j后看到结果是
2.14.1
看起来和我们之前找到的原因相同。
附:SpringBoot运行报错:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookController': Unsatisfied dependency expressed through field 'bookService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookService': Unsatisfied dependency expressed through field 'bookMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookMapper' defined in file [D:tsgltargetclassescombytsglmapperBookMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse config resource: class path resource [mybatis/mybatis-config.xml]; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.logging.LogException: Error setting Log implementation. Cause: java.lang.reflect.InvocationTargetException



