在本地跑一个项目,突然报了一个conflicts with existing, non-compatible bean definition of same name and class的异常,看异常的意思,就是Spring容器扫描类路径下面的bean,发现有相同的bean名称,但是类不相同的两个bean。
根据重复的bean名称进行全局搜索,发现怎么也找不到重复的bean,只好debug来发现,通过异常信息,看到了抛出异常是在spring的doScan()扫描bean方法,这个方法是前期spring通过扫描类路径,并把对应带有Spring注解生成为bean,放在beanDefinitionMap中。
Generic bean: class [com.seewo.care.schools.web.service.impl.ClassBoardMsgProducerImpl]; scope=singleton; abstract=false; lazyInit=false; autowireMode=1; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [/Users/cvte/IdeaProjects/zp-2.6/easicare-performance/performance-schools/target/school-performance/WEB-INF/classes/com/seewo/care/schools/web/service/impl/ClassBoardMsgProducerImpl.class]
Generic bean: class [com.seewo.care.performance.school.administrator.service.impl.ClassBoardMsgProducerImpl]; scope=singleton; abstract=false; lazyInit=false; autowireMode=1; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [jar:file:/Users/cvte/IdeaProjects/zp-2.6/easicare-performance/performance-schools/target/school-performance/WEB-INF/lib/performance-server-2.7.22.jar!/com/seewo/care/performance/school/administrator/service/impl/ClassBoardMsgProducerImpl.class]
通过debug日志,终于发现了重复的bean,原来是代码里面有两个分支,一个是2.6的分支,一个是2.4的分支,2.4的分支把对应的类给迁移到另一个包,后续再合并到stable分支,后面因为2.6没有合并2.4的分支,就在本地运行项目,再合并2.4的分支,这时候代码没有重新编译,编译后的包就有两个相同的类,所以导致spring启动的时候报了bean冲突的异常,把项目的编译包删除再重新编译就可以了。