标签:
Struts2可以完成构造函数的循环依赖注入,来看看Struts2的大师们是怎么做到的吧!
首先定义IBlood与BloodImpl类:
public interface IBlood { } public class BloodImpl implements IBlood{ private IPeople people; @Inject public BloodImpl(@Inject IPeople people) { System.out.println("Blood 构造函数被调用."); this.people = people; } }
再定义个IPeople与PeopleImpl类:
public interface IPeople { } public class PeopleImpl implements IPeople{ private IBlood blood; @Inject public PeopleImpl(@Inject IBlood blood){ System.out.println("People 构造函数被调用 "); this.blood = blood; } }
为什么要为两个实现类定义接口呢?因为两者的依赖注入需要使用JDK的动态代码,而JDK的动态代码需要使用接口来实现。
标签:
原文地址:http://www.cnblogs.com/extjs4/p/5778660.html