【DEBUG】记录weixin-java-mp启动时出现“找不到 xxxx”的问题
文章目录
背景
把微信扫码登录的功能从一个项目迁移到另一个项目中,
框架SpringBoot,使用开源项目weixin-java-mp
<!--微信公众号 SDK-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.4.0</version>
</dependency>在相关代码粘贴好之后,启动时出现:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'districtServiceImpl':
Unsatisfied dependency expressed through field 'webUserService';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'webUserServiceImpl' defined in file [/Users/lyx/code/smart-community/api/target/classes/club/yunzhi/smartcommunity/service/WebUserServiceImpl.class]:
Unsatisfied dependency expressed through constructor parameter 2;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'weChatMpService':
Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError:
me/chanjar/weixin/common/api/WxMessageInMemoryDuplicateCheckerSingleton为了便于观看,已经进行手动换行,分析报错信息得知,出现依赖注入问题,
异常类型:java.lang.NoClassDefFoundError
districtServiceImpl -> webUserService -> weChatMpService -> WxMessageInMemoryDuplicateCheckerSingleton失败
最终原因是这个类WxMessageInMemoryDuplicateCheckerSingleton找不到
排查
(提示:代码不用细看,每个人遇到的问题不完全一样)
上面的注入过程中,除了WxMessageInMemoryDuplicateCheckerSingleton是内部类,其他都是自己写的类
简单检查了一下调用过程,直接搜索报错的类搜不到结果,
然后往回找,在weChatMpService中有一个 new WxMpMessageRouter的操作(用于处理不同类型的微信消息):
final WxMpMessageRouter newRouter = new WxMpMessageRouter(this);而这个WxMpMessageRouter的构造函数中出现了报错信息中的类:
this.messageDuplicateChecker = WxMessageInMemoryDuplicateCheckerSingleton.getInstance();这就找到了问题具体的位置,但内部类的问题,我们是没办法直接解决的。
然后去 Gooooooogle, 在GitHub 的项目 issue 中找到了类似的问题:

虽然包名不一样但原因相同,开发者也给出了原因:

想到这个项目以前虽然没使用过公众号的组件,但用过小程序的组件,
仔细一看自己项目的 pom 发现,公众号和小程序的组件的版本确实不一样
由于先引入的小程序,所以 maven 的库就变成了这样:
所以才导致了公众号组件对 common 组件调用失败
解决办法:保持版本一致
本着“最小修改”的原则,计划把公众号组件和已有版本保持一致,改回4.1版本
<!--微信公众号 SDK-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.1.0</version>
</dependency>
<!--微信公众号 SpringBoot连接器-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>wx-java-mp-spring-boot-starter</artifactId>
<version>4.1.0</version>
</dependency>然后pom.xml -> maven -> 重新加载项目
启动成功:
总结
当使用com.github.binarywang的开源微信组件家族时,需要保持多个组件的版本相同,否则就会出现 common 包和某个具体的包版本不一致的情况,进而导致某些类的依赖注入失败
最后留个开源项目地址:https://github.com/Wechat-Gro...
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论