mybatis Circular dependencies error
博客专区 > Lione0 的博客 > 博客详情
mybatis Circular dependencies error
Lione0 发表于10个月前
mybatis Circular dependencies error
  • 发表于 10个月前
  • 阅读 17
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】买域名送云解析+SSL证书+建站!>>>   

现象

前一段时间开发新项目(springboot1.4.0,mybatis3.2.8,mybatis-spring1.2.1,druid1.0.18)
项目框架搭建完,投入开发一段时间后,有拆库需求,加入多数据源warn警告

2017-06-27 14:34:39.468  INFO 16144 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'defaultDataSourceConfig' of type [class com.diditech.iov.config.DruidDBConfig$$EnhancerBySpringCGLIB$$bdaf88b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-06-27 14:34:39.570  WARN 16144 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceMapper' defined in file [***\ResourceMapper.class]: Cannot resolve reference to bean 'defaultSqlSessionFactoryBean' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'defaultSqlSessionFactoryBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
2017-06-27 14:34:39.571  WARN 16144 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleGroupMapper' defined in file [***\RoleGroupMapper.class]: Cannot resolve reference to bean 'defaultSqlSessionFactoryBean' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'defaultSqlSessionFactoryBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
more...

检查druid配置(如下),mapper文件等,均未发现问题

@MapperScan(basePackages = { "abc.repository", "abc.po" }, sqlSessionFactoryRef = "defaultSqlSessionFactoryBean")
public class DruidDBConfig {  

	/**
	 * 略...
	 * */

	@Bean(name = "defaultSqlSessionFactoryBean")
	@Primary
	public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("defaultDataSource") DataSource dataSource) throws Exception {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(dataSource);
		sqlSessionFactoryBean.setFailFast(true);
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		Resource[] mapperLocations = resolver.getResources("classpath*:/abc/*Mapper.xml");
		sqlSessionFactoryBean.setMapperLocations(mapperLocations);
		//注册mybatis拦截器
		sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageInterceptor()});
		return sqlSessionFactoryBean.getObject();
	}
	
	/**
	 * 略...
	 * */
}

分析 

mapper创建时因defaultSqlSessionFactoryBean bean循环引用不能正确引用异常
项目使用mybatis的@MapperScan对mapper.java进行扫描并向mybatis注册id

遂进入mybatis-spring官网,由指引进入其git(链接),发现issues:#186
使用版本过低,项目已在版本1.2.4已修复该bug

解决

升级mybatis-spring版本后,运行不在有warm循环引用异常

总结建议 

使用最新的用量较高的稳定版本,个人方法:搜索"maven mybatis-spring" (百度也可以)

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 4
码字总数 1906
×
Lione0
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: