摘要: Error creating bean with name 'XXX': Requested bean is currently in creation: Is there an unresolvable circular reference?; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException:

如果把MapperScan单独配置,就不会有警告

DataSource,SqlSessionFactory,MapperScan有依赖关系.

如果把MapperScan单独配置,就不会有警告,例如:

code:

DataSource:

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import javax.sql.DataSource; @Configuration
public class DataSourceConfig { @ConfigurationProperties("spring.datasource.druid")
@Bean
public DataSource equipDataSource() {
return DruidDataSourceBuilder.create().build();
} }

SqlSessionFactory:

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource; @SuppressWarnings("SpringJavaAutowiringInspection")
@Configuration
public class MyBatisConfig { @Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setTypeAliasesPackage("com.app.domain.entity");// 指定基包
factoryBean.setMapperLocations(resolver.getResources("classpath:mapper/**/*.xml"));//
return factoryBean.getObject();
} }

MapperScannerConfigurer

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.mapper.common.BaseMapper;
import tk.mybatis.spring.mapper.MapperScannerConfigurer; import java.util.Properties; @Configuration
public class MapperConfig { @Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.app.domain.mapper");
Properties properties = new Properties();
// 这里要特别注意,不要把BaseMapper放到 basePackage 中,也就是不能同其他Mapper一样被扫描到。
properties.setProperty("notEmpty", "false");
properties.setProperty("IDENTITY", "MYSQL");
mapperScannerConfigurer.setProperties(properties);
mapperScannerConfigurer.setMarkerInterface(BaseMapper.class);
return mapperScannerConfigurer;
} }
 

https://my.oschina.net/doctor2014/blog/386431

Requested bean is currently in creation: Is there an unresolvable circular reference?

getBean的时候由于bean之间存在循环依赖出现类似的错误,先做一个简单实验模拟一下这个异常出现的原因:

bean.xml配置如下:
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
       default-autowire="byName">
<bean id="a" class="BeanA" scope="prototype"/>
<bean id="b" class="BeanB" scope="prototype"/>
</beans>
 
BeanA类如下:
public class BeanA 
{
private BeanB b;
public BeanB getB() {
return b;
}
public void setB(BeanB b) {
this.b = b;
}
}
 
BeanB类如下:
public class BeanB 
{
private BeanA a;
public BeanA getA() {
return a;
}
public void setA(BeanA a) {
this.a = a;
}
}
程序执行的堆栈信息:

bean在初始化的同时会初始化相应的属性(byName),a在初始化的时候会创建a.b,a.b在初始化的时候会创建a.b.a,形成了一个环,所以在AbstractBeanFactory.doGetBean(这里会出现递归调用) 时候 ,走到:

if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
会抛出一个异常:Requested bean is currently in creation: Is there an unresolvable circular reference,然后scope是singlton或者其他的时候bean的创建过程不一样,不会有一个这样子的判断,因而不会抛出这样子的错误。
因为scope=prototype,所以每次请求对应不同的bean,所以在创建的时候有一个依赖的先后顺序,而如果bean配置成singleton,则属性间初始化的顺序可以无所谓了,所有的bean都只有一份,所以在bean是singleton时候不会报unresolvable circular reference的错误。

spring mybatis circular reference的更多相关文章

  1. springboot 异常: Requested bean is currently in creation: Is there an unresolvable circular reference?

    2018-07-31 11:56:18.812 WARN 10316 --- [ main] ConfigServletWebServerApplicationContext : Exception ...

  2. Requested bean is currently in creation: Is there an unresolvable circular reference?

    spring容器初始化报错:循环依赖,错误信息如下: Requested bean is currently in creation: Is there an unresolvable circula ...

  3. SpringMVC +Spring + MyBatis + Mysql + Redis(作为二级缓存) 配置

    转载:http://blog.csdn.net/xiadi934/article/details/50786293 项目环境: 在SpringMVC +Spring + MyBatis + MySQL ...

  4. Spirng 循环依赖报错:Requested bean is currently in creation: Is there an unresolvable circular reference?

    1:前言 最近在项目中遇到了一次循环依赖报错的问题,虽然解决的很快,但是有些不明白的地方,特此记录. 在此我把 bean 的结构和 注入方式单独拎出来进行演示 1.1:报错提示 1.2:错误日志 Ex ...

  5. SpringMVC + Spring + MyBatis 整合 + Spring shrio + easyUI + 权限管理框架,带shrio session和shrio cache集群实现方案

    工作之余先来写了一个不算规范的简单架子 基于spring mvc + spring + mybatis + Spring shrio 基于redis的集群方案 系统权限部分,分成多个机构,其中每个机构 ...

  6. Idea SpringMVC+Spring+MyBatis+Maven调整【转】

    Idea SpringMVC+Spring+MyBatis+Maven整合   创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetyp ...

  7. SpringMVC+Spring+MyBatis+Maven调整【转】

    Idea SpringMVC+Spring+MyBatis+Maven整合   创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetyp ...

  8. struts2 spring mybatis 整合(test)

    这几天搭了个spring+struts2+mybatis的架子,练练手,顺便熟悉熟悉struts2. 环境:myEclipse10+tomcat7+jdk1.6(1.8的jre报错,所以换成了1.6) ...

  9. Spring+Mybatis基于注解整合Redis

    基于这段时间折腾redis遇到了各种问题,想着整理一下.本文主要介绍基于Spring+Mybatis以注解的形式整合Redis.废话少说,进入正题. 首先准备Redis,我下的是Windows版,下载 ...

随机推荐

  1. autohotkey excel getfullname (ComObjActive)

  2. php 如何写一个自己项目的安装程序

    版权声明:此篇文章只是用作笔记,如果版权冲突,请邮件通知一下(15201155501@163.com) https://blog.csdn.net/shenpengchao/article/detai ...

  3. eclipse 更换国内镜像

    大家在用eclipse下载插件,或更新插件的时候,有木有觉得速度贼慢,蜗牛似的速度简直让习惯了4G时代的我们抓狂到底,废话不说,先给大家奉献解决办法 网上找到的国内镜像总结: 1.企业贡献: 搜狐开源 ...

  4. MM常用的双关语(男士必读)

    我们还是当朋友好了 (其实你还是有多馀的利用价值)我想我真的不适合你(我根本就不喜欢你.)天气好冷喔,手都快结冰了 (快牵我的手吧,大木头!)我觉得我需要更多一点的空间 (我不太想看到你啦!)其实你人 ...

  5. complex query几个原则

    1.一般来说in比exists更有利(更容易变成join). 2.尽量避免union,使用union all代替,避免sort. 3,绝对不能在没有on条件下使用join(除非有特殊目的). 4.ou ...

  6. 【Codeforces Round #439 (Div. 2) B】The Eternal Immortality

    [链接] 链接 [题意] 求b!/a!的最后一位数字 [题解] b-a>=20的话 a+1..b之间肯定有因子2和因子5 答案一定是0 否则暴力就好 [错的次数] 在这里输入错的次数 [反思] ...

  7. cocos2d-html5 javascript 通过C++绑定,调用java方法

    1.java中的写法 /* * XConnectPlugin.java * * Created on: 2014年4月30日 * Author: lswdonald9@gmail.com */ pac ...

  8. [Recompose] Compute Expensive Props Lazily using Recompose

    Learn how to use the 'withPropsOnChange' higher order component to help ensure that expensive prop c ...

  9. Html中CSS之去除li前面的小黑点,和ul、LI部分属性方法

    对于很多人用div来做网站时,总会用到,但在显示效果时前面总是会有一个小黑点,这个令很多人头痛,但又找不到要源,其它我们可以用以下方法来清除.1.在CSS中写入代码.找到相关性的CSS,在..li和. ...

  10. 号外:小雷将开发一款Java版的简易CMS系统

    我的个人官网: http://FansUnion.cn 已经改版,隆重上线了,欢迎关注~持续升级中...     出于个人兴趣.技术总结.工作相关,我终于想要做一个简单的CMS系统了. 原来想研究,D ...