处理一个MyBatis初始化失败导致Spring Boot无法启动的问题

最近要新增一些功能到Spring Boot所开发的微服务上。加完之后,启动服务失败!错误提示:

2019-05-20 15:49:18.926 DEBUG 7604 --- [  restartedMain] o.s.w.c.s.StandardServletEnvironment     : Replacing [servletContextInitParams] PropertySource with [servletContextInitParams]
2019-05-20 15:49:19.398  WARN 7604 --- [  restartedMain] com.jebms.albc.config.MybatisConfig      : mybatis resolver mapper*xml is error
2019-05-20 15:49:19.440  WARN 7604 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'albcEslipController': Unsatisfied dependency expressed through field 'eslipService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'albcEslipServiceImpl': Unsatisfied dependency expressed through field 'eslipDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'albcEslipDao' defined in file [D:\JSP_MyEclipse\xygerp-api\xygerp-albc\target\classes\com\jebms\albc\dao\AlbcEslipDao.class]: Unsatisfied dependency expressed through bean property 'sqlSessionTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionTemplate' 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.mybatis.spring.SqlSessionTemplate]: Factory method 'sqlSessionTemplate' threw exception; nested exception is java.lang.NullPointerException
2019-05-20 15:49:19.443  INFO 7604 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'albcEslipController': Unsatisfied dependency expressed through field 'eslipService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'albcEslipServiceImpl': Unsatisfied dependency expressed through field 'eslipDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'albcEslipDao' defined in file [D:\JSP_MyEclipse\xygerp-api\xygerp-albc\target\classes\com\jebms\albc\dao\AlbcEslipDao.class]: Unsatisfied dependency expressed through bean property 'sqlSessionTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionTemplate' 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.mybatis.spring.SqlSessionTemplate]: Factory method 'sqlSessionTemplate' threw exception; nested exception is java.lang.NullPointerException

核心关键字错误:
Factory method 'sqlSessionTemplate' threw exception; nested exception is java.lang.NullPointerException
 

其实类似的问题出现了几次了。
如果单单按照错误(eslipService出错)来找问题,估计找几天几夜都找不到。
还好有经验了。
问题的原因简单来说就是:您写的Mapper.xml解析出异常,导致了MyBaits初始化Session Template失败。
然后,出错就会很可恶地提示第一个Mapper初始化失败(这个解析了为什么会一直提示这个没问题的xml是有异常):

如果你以为是它导致的错误,那根本无法解决,因为提示的这个mpper根本是木有问题的。(这个程序也上线了蛮久了,有问题也不会是现在才出现!)
总结问题:
我们需要知道的是,同一个项目,只有有其中1个Mapper初始化失败,那整体都失败的。所以,关键的问题是,必须要找到是哪个Mapper初始化失败了!一般来说,你只需要聚焦于你最近改了哪个Mapper,认真核对里面是否有一些违规字符即可。
我觉得MyBatis最最最坑爹的就是这里!稍微不注意就会出现问题,而且还没友好的提示,很容易入坑!!反正每次修改xml文件我都得小心翼翼的,唉。
所以,根据排查,找出问题:我最近修改了用户登录的验证(AlbcUserMapper.xml),而且刚刚好用了一个小于号,导致问题。

知道问题是出现在这里,那解决问题就很简单了:

修改一下,套一个转义就行: <![CDATA[ xxxxx ]]>

然后再启用服务,正常了终于。。。。

2019-05-20 16:05:14.035  INFO 7444 --- [  restartedMain] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483647
2019-05-20 16:05:14.036  INFO 7444 --- [  restartedMain] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
2019-05-20 16:05:14.043  INFO 7444 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_XYGERP-ALFSC/192.168.88.31:8183: registering service...
2019-05-20 16:05:14.072  INFO 7444 --- [  restartedMain] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
2019-05-20 16:05:14.103  INFO 7444 --- [  restartedMain] s.d.s.w.s.ApiListingReferenceScanner     : Scanning for api listing references
2019-05-20 16:05:14.367  INFO 7444 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_XYGERP-ALFSC/192.168.88.31:8183 - registration status: 204
2019-05-20 16:05:14.989  INFO 7444 --- [  restartedMain] .d.s.w.r.o.CachingOperationNameGenerator : Generating unique operation named: importPayUsingPOST_1
2019-05-20 16:05:15.247  INFO 7444 --- [  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8183 (http)
2019-05-20 16:05:15.248  INFO 7444 --- [  restartedMain] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8183
2019-05-20 16:05:15.254  INFO 7444 --- [  restartedMain] com.jebms.Application                    : Started Application in 18.096 seconds (JVM running for 18.818)

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
当前课程中商城项目的实战源码是我发布在 GitHub 上的开源项目 newbee-mall (新蜂商城),目前已有 9900 多个 Star,本课程是一个 Spring Boot 技术栈的实战类课程,课程共分为 3 大部分,前面两个部分为基础环境准备和相关概念介绍,第三个部分是 Spring Boot 商城项目功能的讲解,让大家实际操作并实践上手一个大型的线上商城项目,并学习到一定的开发经验以及其中的开发技巧。商城项目所涉及的功能结构图整理如下: 作者寄语本课程录制于2019年,距今已有一段时间。期间,Spring Boot技术栈也有一些版本升级,比如Spring Boot 2.7.x发版、Spring Boot 3.x版本正式版本。对于这些情况,笔者会在本课程实战项目的开源仓库中创建不同的代码分支,保持实战项目的源码更新,保证读者朋友们不会学习过气的知识点。新蜂商城的优化和迭代工作不会停止,不仅仅是功能的优化,在技术栈上也会不断的增加,截止2023年,新蜂商城已经发布了 7 个重要的版本,版本记录及开发计划如下图所示。 课程特色 对新手开发者十分友好,无需复杂的操作步骤,仅需 2 秒就可以启动这个完整的商城项目最终的实战项目是一个企业级别的 Spring Boot 大型项目,对于各个阶段的 Java 开发者都是极佳的选择实践项目页面美观且实用,交互效果完美教程详细开发教程详细完整、文档资源齐全代码+讲解+演示网站全方位保证,向 Hello World 教程说拜拜技术栈新颖且知识点丰富,学习后可以提升大家对于知识的理解和掌握,可以进一步提升你的市场竞争力 课程预览 以下为商城项目的页面和功能展示,分别为:商城首页 1商城首页 2购物车订单结算订单列表支付页面后台管理系统登录页商品管理商品编辑
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值