package com.xunhuan; public class DemoA { private DemoB b; public DemoB getB() { return b; } public void setB(DemoB b) { this.b = b; } public DemoA(){ System.out.println(" ---DemoA created success "); } }
package com.xunhuan; public class DemoB { private DemoA a; public DemoA getA(){ return a; } public void setA(DemoA a) { this.a = a; } public DemoB(){ System.out.println(" ----DemoB created success"); } }
<?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 name="a" class="com.xunhuan.DemoA" scope="singleton"> <property name="b" ref="b" /> </bean> <bean name="b" class="com.xunhuan.DemoB" scope="singleton"> <property name="a" ref="a" /> </bean> </beans>
package com.xunhuan; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class CilentSpringContainer { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); DemoA a = applicationContext.getBean("a", DemoA.class); DemoB b = applicationContext.getBean("b", DemoB.class); } }
<?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 name="a" class="com.xunhuan.DemoA" scope="singleton"> <property name="b" ref="b" /> </bean> <bean name="b" class="com.xunhuan.DemoB" scope="singleton"> <property name="a" ref="a" /> </bean> </beans>
spring循环依赖使用singleton 单例模式正常运行
<?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 name="a" class="com.xunhuan.DemoA" scope="prototype"> <property name="b" ref="b" /> </bean> <bean name="b" class="com.xunhuan.DemoB" scope="prototype"> <property name="a" ref="a" /> </bean> </beans>
spring循环依赖使用原型 prototype 不支持循环依赖,使用时 则会抛出异常 (BeanCurrentlyInCreationException) Requested bean is currently in creation: Is there an unresolvable circular reference?