序文
私たちは、春は遅延ロードすることができることを知って、それがビーンインスタンス化されたときに本当の豆を使用したときです。もちろん、すべてのように、構成例怠惰-INIT属性Beanの、あなたは春の負荷のタイミングを制御することができません。今のマシンのパフォーマンス、メモリなどが比較的高い、Beanが実際に業務用に取得されたとき、あなたは負担の多くを減らすことができるように基本的には、少し長いBeanコンテナをロードするために、ブート時に遅延読み込みを使用し、起動時間はありません、後で分析を行います。私たちは、豆、Factroyから取得するための最も直接的な方法に時間を使い、これはソースBeanインスタンスをロードすることです。
循環依存関係、または周期依存のソリューションを達成するためにどのように春:最近の問題を発見し、いくつかの大企業(よく知られている会社ので、いくつかのボスレベル)は、インタビューの過程で、基本的な質問をします。この問題に関する主な議論は、単に次の日を行うには:
3簡単な豆:TESTA、TESTB、TESTC AがBを含み、C、C含有A.含むB バネは次いで、3つの容器に注入されます。次のように:
クラスA:
public class TestA {
private TestB testB;
public TestB getTestB() {
return testB;
}
public void setTestB(TestB testB) {
this.testB = testB;
}
}
クラスB:
public class TestB {
private TestC testC;
public TestC getTestC() {
return testC;
}
public void setTestC(TestC testC) {
this.testC = testC;
}
}
クラスC:
public class TestC {
private TestA testA;
public TestA getTestA() {
return testA;
}
public void setTestA(TestA testA) {
this.testA = testA;
}
}
単にXMLとして春に注入:
<!-- 循环依赖测试 -->
<bean id="testA" class="com.xin.learn.xhyl.vo.TestA" scope="prototype">
<property name="testB" ref="testB"></property>
</bean>
<bean id="testB" class="com.xin.learn.xhyl.vo.TestB" scope="prototype">
<property name="testC" ref="testC"></property>
</bean>
<bean id="testC" class="com.xin.learn.xhyl.vo.TestC" scope="prototype">
<property name="testA" ref="testA"></property>
</bean>
テストカテゴリ:
public class TestMain {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
System.out.println(context.getBean("testA", TestA.class));
}
}
あなたがWebプロジェクトであれば、文句はありませんプロジェクトを実行しますが、参照されたときにするとき、またはテストクラス見つかったエラーを実行します。
Error creating bean with name 'testA' defined in class path resource [spring/applicationContext.xml]: Cannot resolve reference to bean 'testB' while setting bean property 'testB'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testB' defined in class path resource [spring/applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.xin.learn.xhyl.vo.TestB' to required type 'com.xin.learn.xhyl.vo.TestC' for property 'testC'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.xin.learn.xhyl.vo.TestB] to required type [com.xin.learn.xhyl.vo.TestC] for property 'testC': no matching editors or conversion strategy found
大雑把意味:あなたは種皮を作成し、TESTBを参照しないプロパティTESTBを設定した場合。
この時間なのでTESTBが作成されていません。
解像度:シングルトン、範囲、または削除する範囲の値が通常の動作に、(デフォルトスプリングによっては、Beanの単一の実施形態です)。理由:
春はEarlyBeanReference機能を提供し、春の同時マップsingletonObjectsのための最初の名前を持つすべてのインスタンス化と初期化の良い豆を格納するために使用され、singletonFactoriesは循環依存豆情報(のbeanName、およびコールバック機能を解決するために必要な格納するために使用されます)。beanAをインスタンス化するとき、それが引き金となるときgetBean(“beanA”);
、beanAでsingletonObjectsは確かにそうbeanAインスタンス化されていない初めに、戻ってきているかどうかを確認するために最初にあなたが設定している場合allowCircularReferences=true
(デフォルトはtrue)と単一の部品として、現在のBeanとBeanが作成中です、単一の部品singletonFactories内部に地図情報にBeanのプロパティを初期化する前に。インスタンスはBeanB噴射属性次に、噴射時間プロパティは、あろうgetBean(“beanB”)
BeanB singletonObjectsは、それがBeanBをインスタンス化し、次いでsingletonFactoriesを配置し、次に注射BeanA属性ではない場合、トリガを見つけるgetBean(“beanA”);
この時点では、(1)getSingletonリターンがインスタンスになるであろうbeanA。このbeanBに追加戻る、その後singletonObjectsにbeanAを追加し、その後、beanAの初期化が完了すると、返された後、singletonObjectsにbeanBを初期化します。