..


スポンサーリンク

コアコンテナ - コンストラクタの依存性注入

今コンストラクタの依存性注入を使用する方法を、例によって、見てみましょう
我々は、汎用サービスに依存する私たちのBeanを作成します。






 it.mrwebmaster.di.constructorパッケージ;









パブリッククラスビーン{





  



プライベートのGenericServiceののGenericService;



	

  



プライベート文字列beanNameを。





  



 / **



   



 *メーカー



   



 * @ ParamでのGenericService



   



 * @ ParamでbeanNameを



   



 * /



  



公共ビーン(のGenericServiceのGenericService、文字列beanNameは){



    



スーパー();



    



 this.genericService =のGenericService;



    



 this.beanName = beanNameは;



  



 }



	

  



 / **



   



 *パブリックメソッド



   



 * /



  



公共ボイドのdoIt(){



    



 system.out.printlnを(beanNameは+"何かをする");



    



 genericService.dosomething();



  



 }



	





 }



依存関係nell'applicationContext。xmlを設定する





 <bean id="genericService" class="it.mrwebmaster.di.constructor.GenericServiceImpl" scope="singleton" />









 <bean id="constructorBean" class="it.mrwebmaster.di.constructor.bean">



  



 <constructor-arg value="beanName"/>



  



 <constructor-arg ref="genericService"/>







 </ビーン>



コンストラクタの依存性注入は、refやvalue属性を使用してタグ- argコンストラクタを使用する必要があります使用する例からわかるように。 value属性が文字列や数値などのデフォルト値を渡すために使用されている間REF Attibassiは、既にnell'IoCの別のBeanのコンテナをインスタンス化引数として渡す必要があります。

この例ではクラスのコンストラクタは、入力としてのGenericService豆と文字列を受け付けますが、彼は春は彼らのタイプして引数を組み合わせる方法を示すために引数の順序を逆にしたい例。
この種の動作は、すべての引数の型が異なる場合は正常ですが、引数が同じ型であればどのように我々は、順序を指定すればいいのですか? タグのargコンストラクタはパラメータを渡すと春の順序を示すインデックスの属性が用意されています。






 <bean id="constructorBean" class="it.mrwebmaster.di.constructor.bean">



  



 <constructor-arg value="beanName" index="1" />



  



 <constructor-arg ref="genericService" index="0" />







 </ビーン>



別のケースでは、construttoreは、例えば、両方の文字列で表すことのできる入力つのパラメータとして受け入れるにはあいまいなの1つである可能性があります。






公共ビーン(のGenericServiceのGenericService、文字列beanNameは、invocationTimes整数){



  



スーパー();



  



 this.genericService =のGenericService;



  



 this.beanName = beanNameは;



  



 this.invocationTimes = invocationTimes;







 }



とnell'applicationContext。xmlの





 <bean id="constructorBean2" class="it.mrwebmaster.di.constructor.bean">



  



 <constructor-arg value="0" />という



  



 <constructor-arg value="beanName" />



  



 <constructor-arg ref="genericService" />







 </ビーン>



この場合、"0"のようなエラーから春には文字列または数値のいずれかを指定できます。 この問題を解決するには、インデックスまたはattriburoこのようにtype属性を使用します。





 <bean id="constructorBean2" class="it.mrwebmaster.di.constructor.bean">



  



 <constructor-arg type="java.lang.Integer" value="0" />という



  



 <constructor-arg value="beanName" type="java.lang.String" />



  



 <constructor-arg ref="genericService" />







 </ビーン>



属性のコンストラクタ- argはまた、ファクトリメソッドにパラメータを渡すために使用することができます。






公共の静的な豆createBean(のGenericServiceのGenericService、文字列beanNameは、invocationTimes整数){



  



ビーンb =新しい豆(のGenericService、beanNameは、invocationTimes);



  



 / / somethigください.......



  



 bを返す。







 }



nell'applicationContext。xmlの





 <bean id="constructorBean3" class="it.mrwebmaster.di.constructor.bean" factory-method="createBean">



  



 <constructor-arg type="java.lang.Integer" value="0" />という



  



 <constructor-arg value="beanName" type="java.lang.String" />



  



 <constructor-arg ref="genericService" />







 </ビーン>



スプリングJavaのガイド
E -ラーニング
Linuxの(コース) Linuxの(コース)
オープンソースシステムへの完全なガイド。 49€から。
PHP(コース) PHP(コース)
動的なWebサイトを作成するためのフルコース。 49€から。
RubyとRuby on Rails(コース) RubyとRuby on Rails(コース)
RubyとRoRのと、ソフトウェアやWebアプリケーションを作成します。 39€から。
スポンサーリンク