背景
A項目之前一直都是好好的,但是某天下午啟動的時候報錯,報錯見下,報錯原因是org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.apache.ibatis.session.SqlSessionFactory' available: more than one 'primary' bean found among candidates。
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionTemplate' defined in class path resource [tk/mybatis/mapper/autoconfigure/MapperAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionTemplate' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.apache.ibatis.session.SqlSessionFactory' available: more than one 'primary' bean found among candidates: [aSqlSessionFactory, bSqlSessionFactory] at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:732) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:474) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1256) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1105) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
定位問題
初步定位這個問題的時候眼睛一直盯着more than one 'primary' bean found among candidates: [aSqlSessionFactory, bSqlSessionFactory],然后去看了一下A項目中的mybatis配置,發現都是正常的,這里一直也沒有人改動過。
所以項目啟動的時候為什么會掃出來bSqlSessionFactory呢?
我嘗試把代碼切換到master分支上,發現又是可以正常啟動的,所以可以肯定應該是當前分支哪里出現了問題,然后和master分支的代碼進行了對比,沒有做什么改動。。。
但是項目及依賴的版本號是不同的!!!
當前分支使用的是SNAPSHOT的版本號,然后想到了bSqlSessionFactory是配置在B項目中的,難道是包依賴出現了問題?
A項目依賴B項目的facade包,拉下來最新的B項目的代碼,發現最近一次有人對B項目的facade的依賴進行了調整,新增了一個對B項目的dao包的依賴,這樣啟動A項目啟動的時候就會把B項目的mybatis配置也掃描進來,從而發現了兩個sqlSessionFactory導致了NoUniqueBeanDefinitionException。
然后找到了相關人員確定了一下后發現是誤加上去的,最后把B項目的facade中依賴B自身的dao包這個依賴關系給去掉后就可以正常啟動a項目了。