Spring Error : No unique bean of type [org.apache.ibatis.session.SqlSessionFactory] is defined

时间:2021-03-18 22:22:37

报错信息:

Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public final void org.mybatis.spring.support.SqlSessionDaoSupport.setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.apache.ibatis.session.SqlSessionFactory] is defined: expected single matching bean but found 2...
 

原因:

beans.xml 中定义了两个 SqlSessionFactory bean,而在 SqlSessionDaoSupport 中调用了下面 setSqlSessionFactory(),造成了不唯一。
 
1 @Autowired(required = false)
2 public final void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
3 if (! this.externalSqlSession ) {
4 this. sqlSession = new SqlSessionTemplate(sqlSessionFactory);
5 }
6 }
 

解决方案:

修改 beans.xml 中与 SqlSessionFactory bean 相关的配置。