Spring 循环依赖

 

目录

1、概述

2、构造器循环依赖错误示例

创建三个循环依赖类

spring配置

spring引导类

运行结果

分析

3、setter 单例模式下循环依赖示例

循环依赖类

spring配置

引导类

运行结果

分析

4、prototype循环依赖错误示例

上个例子改下spring配置

运行结果

分析


1、概述

循环依赖就是循环引用,例如 A 中有 B,B 中有 A,形成了一个环状引用,这就是循环依赖。

对于构造器循环依赖,无法解决。

setter 循环依赖 在 single (单例)模式下可以解决。

prototype 模式下无法解决。

2、构造器循环依赖错误示例

创建三个循环依赖类

package thinking.in.spring.boot.samples.spring5.circle;

public class CircleA {

    private CircleB circleB;

    public CircleA(CircleB circleB) {
        this.circleB = circleB;
    }

    public CircleB getCircleB() {
        return circleB;
    }

    public void setCircleB(CircleB circleB) {
        this.circleB = circleB;
    }
}
package thinking.in.spring.boot.samples.spring5.circle;

public class CircleB {

    private CircleC circleC;

    public CircleB(CircleC circleC) {
        this.circleC = circleC;
    }

    public CircleC getCircleC() {
        return circleC;
    }

    public void setCircleC(CircleC circleC) {
        this.circleC = circleC;
    }
}
package thinking.in.spring.boot.samples.spring5.circle;

public class CircleC {

    private CircleA circleA;

    public CircleC(CircleA circleA) {
        this.circleA = circleA;
    }

    public CircleA getCircleA() {
        return circleA;
    }

    public void setCircleA(CircleA circleA) {
        this.circleA = circleA;
    }
}

spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:customLabel="http://www.rh.com/schema/customLabel"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.rh.com/schema/customLabel
        http://www.rh.com/schema/customLabel.xsd">

    <bean id="circleA" class="thinking.in.spring.boot.samples.spring5.circle.CircleA">
        <constructor-arg index="0" ref="circleB" />
    </bean>
    <bean id="circleB" class="thinking.in.spring.boot.samples.spring5.circle.CircleB">
        <constructor-arg index="0" ref="circleC" />
    </bean>
    <bean id="circleC" class="thinking.in.spring.boot.samples.spring5.circle.CircleC">
        <constructor-arg index="0" ref="circleA" />
    </bean>

</beans>

spring引导类

package thinking.in.spring.boot.samples.spring5.bootstrap;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import thinking.in.spring.boot.samples.spring5.bean.User;
import thinking.in.spring.boot.samples.spring5.circle.CircleA;
import thinking.in.spring.boot.samples.spring5.customlabel.CustomLabel;
import thinking.in.spring.boot.samples.spring5.factorybean.UserFactoryBean;

public class CircleBeanFactoryBootstrap {

    public static void main(String[] args) {
        DefaultListableBeanFactory defaultListableBeanFactory = new XmlBeanFactory(new ClassPathResource("application-circle.xml"));
        CircleA circleA = (CircleA) defaultListableBeanFactory.getBean("circleA");
        System.out.println(circleA);
    }
}

运行结果

三月 24, 2020 11:41:40 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [application-circle.xml]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'circleA' defined in class path resource [application-circle.xml]: Cannot resolve reference to bean 'circleB' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'circleB' defined in class path resource [application-circle.xml]: Cannot resolve reference to bean 'circleC' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'circleC' defined in class path resource [application-circle.xml]: Cannot resolve reference to bean 'circleA' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'circleA': Requested bean is currently in creation: Is there an unresolvable circular reference?
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:378)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110)
	at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:622)
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:148)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1274)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1131)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:541)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at thinking.in.spring.boot.samples.spring5.bootstrap.CircleBeanFactoryBootstrap.main(CircleBeanFactoryBootstrap.java:15)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'circleB' defined in class path resource [application-circle.xml]: Cannot resolve reference to bean 'circleC' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'circleC' defined in class path resource [application-circle.xml]: Cannot resolve reference to bean 'circleA' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'circleA': Requested bean is currently in creation: Is there an unresolvable circular reference?
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:378)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110)
	at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:622)
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:148)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1274)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1131)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:541)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
	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.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:367)
	... 12 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'circleC' defined in class path resource [application-circle.xml]: Cannot resolve reference to bean 'circleA' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'circleA': Requested bean is currently in creation: Is there an unresolvable circular reference?
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:378)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110)
	at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:622)
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:148)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1274)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1131)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:541)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
	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.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:367)
	... 24 more
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'circleA': Requested bean is currently in creation: Is there an unresolvable circular reference?
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:345)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:221)
	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.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:367)
	... 36 more

分析

Spring 正在创建 bean 的时候,都会把正在创建的bean放到一个“当前正在创建 bean 池”中,bean创建过程中一直在这个池子中,直到完成 bean的创建才会从池中删除。

CircleA 正在创建会在池中,因为CircleA引用了 CircleB,所以创建 CircleB,CircleB也进入这个池,CircleB引用了CircleC,CircleC也进入这个池中,CircleC引用了 CircleA,发现CircleA也在池中,并没有创建完毕,这是就会报 BeanCurrentlyInCreationException 异常,告诉你循环依赖异常。

3、setter 单例模式下循环依赖示例

对构造器依赖循环示例代码进行改造,加无参构造器

循环依赖类

package thinking.in.spring.boot.samples.spring5.circle;

public class CircleA {

    private CircleB circleB;

    public CircleA() {
    }

    public CircleA(CircleB circleB) {
        this.circleB = circleB;
    }

    public void show() {
        System.out.println("circleA show");
    }

    public CircleB getCircleB() {
        return circleB;
    }

    public void setCircleB(CircleB circleB) {
        this.circleB = circleB;
    }
}
package thinking.in.spring.boot.samples.spring5.circle;

public class CircleB {

    private CircleC circleC;

    public CircleB() {
    }

    public CircleB(CircleC circleC) {
        this.circleC = circleC;
    }

    public void show() {
        System.out.println("circleB show");
    }

    public CircleC getCircleC() {
        return circleC;
    }

    public void setCircleC(CircleC circleC) {
        this.circleC = circleC;
    }
}
package thinking.in.spring.boot.samples.spring5.circle;

public class CircleC {

    private CircleA circleA;

    public CircleC() {
    }

    public CircleC(CircleA circleA) {
        this.circleA = circleA;
    }


    public void show() {
        System.out.println("circleC show");
    }

    public CircleA getCircleA() {
        return circleA;
    }

    public void setCircleA(CircleA circleA) {
        this.circleA = circleA;
    }
}

spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:customLabel="http://www.rh.com/schema/customLabel"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.rh.com/schema/customLabel
        http://www.rh.com/schema/customLabel.xsd">

    <bean id="circleA" class="thinking.in.spring.boot.samples.spring5.circle.CircleA">
        <!--<constructor-arg index="0" ref="circleB" />-->
    </bean>
    <bean id="circleB" class="thinking.in.spring.boot.samples.spring5.circle.CircleB">
        <!--<constructor-arg index="0" ref="circleC" />-->
    </bean>
    <bean id="circleC" class="thinking.in.spring.boot.samples.spring5.circle.CircleC">
        <!--<constructor-arg index="0" ref="circleA" />-->
    </bean>

</beans>

引导类

package thinking.in.spring.boot.samples.spring5.bootstrap;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import thinking.in.spring.boot.samples.spring5.bean.User;
import thinking.in.spring.boot.samples.spring5.circle.CircleA;
import thinking.in.spring.boot.samples.spring5.circle.CircleB;
import thinking.in.spring.boot.samples.spring5.circle.CircleC;
import thinking.in.spring.boot.samples.spring5.customlabel.CustomLabel;
import thinking.in.spring.boot.samples.spring5.factorybean.UserFactoryBean;

public class CircleBeanFactoryBootstrap {

    public static void main(String[] args) {
        DefaultListableBeanFactory defaultListableBeanFactory = new XmlBeanFactory(new ClassPathResource("application-circle.xml"));
        CircleA circleA = (CircleA) defaultListableBeanFactory.getBean("circleA");
        circleA.show();
        CircleB circleB = (CircleB) defaultListableBeanFactory.getBean("circleB");
        circleB.show();
        CircleC circleC = (CircleC) defaultListableBeanFactory.getBean("circleC");
        circleC.show();
    }
}

运行结果

circleA show
circleB show
circleC show

分析

因为单例模型 setter 注入方式,首次getBean("circleA")时,Spring容器提前为 CircleA、CircleB、CircleC 各创建一个 ObjectFactory并缓存起来

this.addSingletonFactory(beanName, new ObjectFactory() {
    public Object getObject() throws BeansException {
       return this.getEarlyBeanReference(beanName, mbd, bean);
    }
});

缓存如下:

再次回调getBean("circleA"),调用如下方法

protected Object getSingleton(String beanName, boolean allowEarlyReference) {
        Object singletonObject = this.singletonObjects.get(beanName);
        if (singletonObject == null && this.isSingletonCurrentlyInCreation(beanName)) {
            Map var4 = this.singletonObjects;
            synchronized(this.singletonObjects) {
                singletonObject = this.earlySingletonObjects.get(beanName);
                if (singletonObject == null && allowEarlyReference) {
// 从这里拿出缓存的 ObjectFactory,this.singletonFactories中有CircleA、CircleB、CircleC的 ObjectFactory
                    ObjectFactory<?> singletonFactory = (ObjectFactory)this.singletonFactories.get(beanName);
                    if (singletonFactory != null) {
// 创建
                        singletonObject = singletonFactory.getObject();
// 缓存 ObjectFactory.getObject() 创建的bean
                        this.earlySingletonObjects.put(beanName, singletonObject);
// 移除缓存的 ObjectFactory
                        this.singletonFactories.remove(beanName);
                    }
                }
            }
        }

        return singletonObject;
    }

 

4、prototype循环依赖错误示例

上个例子改下spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:customLabel="http://www.rh.com/schema/customLabel"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.rh.com/schema/customLabel
        http://www.rh.com/schema/customLabel.xsd">

    <bean id="circleA" class="thinking.in.spring.boot.samples.spring5.circle.CircleA" scope="prototype">
        <!--<constructor-arg index="0" ref="circleB" />-->
        <property name="circleB" ref="circleB" />
    </bean>
    <bean id="circleB" class="thinking.in.spring.boot.samples.spring5.circle.CircleB" scope="prototype">
        <!--<constructor-arg index="0" ref="circleC" />-->
        <property name="circleC" ref="circleC" />
    </bean>
    <bean id="circleC" class="thinking.in.spring.boot.samples.spring5.circle.CircleC" scope="prototype">
        <!--<constructor-arg index="0" ref="circleA" />-->
        <property name="circleA" ref="circleA" />
    </bean>

</beans>

运行结果

"D:\Program Files\Java\jdk1.8.0_201\bin\java" "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.7\lib\idea_rt.jar=58065:D:\Program Files\JetBrains\IntelliJ IDEA 2017.2.7\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.8.0_201\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\cldrdata.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\jfxrt.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\nashorn.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\sunpkcs11.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\ext\zipfs.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\jce.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\jfxswt.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\resources.jar;D:\Program Files\Java\jdk1.8.0_201\jre\lib\rt.jar;F:\github_workspace\thinking-in-spring-boot-samples\spring-framework-samples\spring-framework-5.0.x-sample-rh\target\classes;E:\maven\ali_repo\org\springframework\spring-context\5.0.6.RELEASE\spring-context-5.0.6.RELEASE.jar;E:\maven\ali_repo\org\springframework\spring-aop\5.0.6.RELEASE\spring-aop-5.0.6.RELEASE.jar;E:\maven\ali_repo\org\springframework\spring-beans\5.0.6.RELEASE\spring-beans-5.0.6.RELEASE.jar;E:\maven\ali_repo\org\springframework\spring-core\5.0.6.RELEASE\spring-core-5.0.6.RELEASE.jar;E:\maven\ali_repo\org\springframework\spring-jcl\5.0.6.RELEASE\spring-jcl-5.0.6.RELEASE.jar;E:\maven\ali_repo\org\springframework\spring-expression\5.0.6.RELEASE\spring-expression-5.0.6.RELEASE.jar;E:\maven\ali_repo\org\springframework\spring-tx\5.0.6.RELEASE\spring-tx-5.0.6.RELEASE.jar;E:\maven\ali_repo\org\springframework\spring-context-indexer\5.0.6.RELEASE\spring-context-indexer-5.0.6.RELEASE.jar" thinking.in.spring.boot.samples.spring5.bootstrap.CircleBeanFactoryBootstrap
三月 24, 2020 3:02:43 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [application-circle.xml]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'circleA' defined in class path resource [application-circle.xml]: Cannot resolve reference to bean 'circleB' while setting bean property 'circleB'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'circleB' defined in class path resource [application-circle.xml]: Cannot resolve reference to bean 'circleC' while setting bean property 'circleC'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'circleC' defined in class path resource [application-circle.xml]: Cannot resolve reference to bean 'circleA' while setting bean property 'circleA'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'circleA': Requested bean is currently in creation: Is there an unresolvable circular reference?
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:378)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1609)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1361)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:578)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at thinking.in.spring.boot.samples.spring5.bootstrap.CircleBeanFactoryBootstrap.main(CircleBeanFactoryBootstrap.java:17)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'circleB' defined in class path resource [application-circle.xml]: Cannot resolve reference to bean 'circleC' while setting bean property 'circleC'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'circleC' defined in class path resource [application-circle.xml]: Cannot resolve reference to bean 'circleA' while setting bean property 'circleA'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'circleA': Requested bean is currently in creation: Is there an unresolvable circular reference?
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:378)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1609)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1361)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:578)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:367)
	... 8 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'circleC' defined in class path resource [application-circle.xml]: Cannot resolve reference to bean 'circleA' while setting bean property 'circleA'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'circleA': Requested bean is currently in creation: Is there an unresolvable circular reference?
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:378)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:110)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1609)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1361)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:578)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:367)
	... 16 more
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'circleA': Requested bean is currently in creation: Is there an unresolvable circular reference?
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:264)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:367)
	... 24 more

Process finished with exit code 1

分析

prototype 作用域因为无法提前暴露一个创建中的bean(ObjectFactory),所以会报异常。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值