Spring Autowired 故障处理一则

今天下午耽误了很长时间都在处理Autowired的问题,趁没有睡觉赶快写下来,提醒自己以后处理问题不要再走冤枉路。也为大家做个反面教材,不过中间用到的一些shell命令,也可以帮助大家在找类似问题时提高效率。

异常: 很常见
Could not autowire field: private aaa.bbb.PhotoService com.xiaonei.platform.component.application.like.LikeInternalContentService.photoService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.xiaonei.album.service.PhotoService] is defined: Unsatisfied dependency of type [class com.xiaonei.album.service.PhotoService]: expected at least 1 matching bean


检查过程:
-1. 定位到 aaa.bbb.PhotoService 问题,(这是正确的,以为很快会解决
0. 以为自己的pom.xml依赖问题。
反复检查,没有发现问题。(耽误了时间,开始紧张,感到这个问题很麻烦
1. 检查class path下所有的确实包含。
for i in `ls target/web/WEB-INF/lib/*.jar` ; do echo $i ; unzip -l $i | grep aaa.bbb.PhotoService ;done;
判断为没有注入
2. 检查所有applicationContext*.xml没有问题(步骤是对的,但是没有查全,后来就悲剧了)
mkdir aaa; for i in `ls *.jar` ; do unzip -x $i -d aaa/$i/  ;done;
find . -name \*.xml | xargs grep PhotoService
迷惑。。。。
重复1,2 若干次,耽误时间。(这个时候快崩溃了,以为自己遇到了什么高深问题,还找来了别的同学一起帮忙找,结果其他人也被我的紧张给迷惑了。
以为是容器问题,tomcat初始化顺序问题。换resin。(现在想起来,当时纯属是在碰运气

晚上回家冷静后,想起可能是自己工程定义的同名bean问题(当时没有检测自己工程的applicationContext)。检查果然是。

看似一个小问题,背后暴露了很多大问题,从个人到整个框架,就不再这里说了。