03-Spring3 依赖注入(DI)B_循环依赖【转】
package com.iflytek.demo;public class CircleA {private CircleB circleB;public CircleA() {}public CircleA(CircleB circleB) {this.circleB = circleB;}public void setCircleB(CircleB circleB) {this.circleB = circleB;}public void a() {circleB.b();}}
package com.iflytek.demo;public class CircleB {private CircleC circleC;public CircleB() {}public CircleB(CircleC circleC) {this.circleC = circleC;}public void setCircleC(CircleC circleC) {this.circleC = circleC;}public void b() {circleC.c();}}
package com.iflytek.demo;public class CircleC {private CircleC circleC;public CircleC() {}public CircleC(CircleC circleC) {this.circleC = circleC;}public void setCircleC(CircleC circleC) {this.circleC = circleC;}public void c() {circleC.c();}}
<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"><bean id="circleA" ref="circleB" /></bean><bean id="circleB" ref="circleC" /></bean><bean id="circleC" ref="circleA" /></bean></beans>
package com.iflytek.test;import org.junit.Test;import org.springframework.beans.factory.BeanCurrentlyInCreationException;import org.springframework.context.support.ClassPathXmlApplicationContext;public class CircleTest {@Test(expected = BeanCurrentlyInCreationException.class)public void testCircleByConstructor() throws Throwable {try {new ClassPathXmlApplicationContext("circle.xml");} catch (Exception e) {// 因为要在创建circle3时抛出;Throwable e1 = e.getCause().getCause().getCause();throw e1;}}}
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'circleA': Requested bean is currently in creation: Is there an unresolvable circular reference?
addSingletonFactory(beanName, new ObjectFactory() { public Object getObject() throws BeansException { return getEarlyBeanReference(beanName, mbd, bean); }});
<!-- 定义Bean配置文件,注意scope都是“prototype” --><bean id="circleA" scope="prototype"><property name="circleB" ref="circleB" /></bean><bean id="circleB" scope="prototype"><property name="circleC" ref="circleC" /></bean><bean id="circleC" scope="prototype"><property name="circleA" ref="circleA" /></bean>
@Test(expected = BeanCurrentlyInCreationException.class)public void testCircleBySetterAndPrototype() throws Throwable {try {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("circle2.xml");System.out.println(ctx.getBean("circleA"));} catch (Exception e) {Throwable e1 = e.getCause().getCause().getCause();throw e1;}}
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'circleA': Requested bean is currently in creation: Is there an unresolvable circular reference?circleAorg.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'circleA': Requested bean is currently in creation: Is there an unresolvable circular reference?Error creating bean with name 'circleA': Requested bean is currently in creation: Is there an unresolvable circular reference?null
@Test(expected = BeanCurrentlyInCreationException.class)public void testCircleBySetterAndSingleton2() throws Throwable {try {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();ctx.setConfigLocation("circle3.xml");ctx.refresh();} catch (Exception e) {Throwable e1 = e.getCause().getCause().getCause();throw e1;}}
org.springframework.beans.NotWritablePropertyException: Invalid property 'circleA' of bean class [com.iflytek.demo.CircleC]: Bean property 'circleA' is not writable or has an invalid setter method. Did you mean 'circleC'?
?