处理一个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是有异常):
处理一个MyBatis初始化失败导致Spring Boot无法启动的问题

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

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

修改一下,套一个转义就行: <![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)