Spring基础(5): 构造函数注入无法处理循环依赖

public class Person{
    public Leader leader;
    public Person(Leader l){
        this.leader= l;
    }
}

public class Leader {
    public Person p;
    public Leader(Person p){
        this.p=p;
    }
}


public class Main {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("a.xml");
    }
}


  <bean id="person" class="com.Person">
        <constructor-arg ref="leader"/>
    </bean>

    <bean id="leader" class="com.Leader">
        <constructor-arg ref="person"/>
    </bean>

无法编译通过,报错。构造函数注入不能循环依赖.

 

posted @ 2017-03-14 21:27  lh_cn  阅读(1433)  评论(0编辑  收藏  举报