项目依赖问题导致No qualifying bean of type 'org.apache.ibatis.session.SqlSessionFactory' available

2023-05-12 20:14:37 浏览数 (3)

背景

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。

代码语言:javascript复制
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项目了。

0 人点赞