春は、一般的にどのような注入方法を使用しましたか?

設定方法注入クラスボウルパッケージconstxiong.interview.inject。パブリッククラスボウル{






















ます。public void putRice(){
System.out.printlnは( "盛饭...");
}
}
クラスPersonの
パッケージconstxiong.interview.inject。
パブリッククラスPerson {
プライベートボウルボウル。
公共ボイドは(){食べる
bowl.putRiceを();
System.out.println( "开始吃饭...");
}
公共ボイドsetBowl(ボウルボウル){
this.bowl =ボウル。
}
}
春配置文件
<xmlのバージョン= "1.0"エンコード= "UTF-8"?>
<豆のxmlns =」http://www.springframework.org/schema/beans "
のxmlns:XSI =" のhttp:// www.w3.org/2001/XMLSchema-instance "
のxmlns:コンテキスト=" のhttp://www.springframework。
XSI:のschemaLocation =」
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd "> ;
<豆ID = "ボウル"クラス= "constxiong.interview.inject.Bowl" />
<豆のid = "人"クラス= "constxiong.interview.inject.Person">
<プロパティ名= "ボウル" REF = "ボウル「> </ property>の
</豆>
</豆>
测试类
パッケージconstxiong.interview.inject。
輸入org.springframework.context.ApplicationContext;
輸入org.springframework.context.support.ClassPathXmlApplicationContext;

静的な無効メインパブリック(文字列[]引数){
ApplicationContextのClassPathXmlApplicationContextコンテキスト=新しい新規( "spring_inject.xml");
人物人物=(人物)context.getBean( "人");
person.eat();
}
}
修飾B、プロファイルクラスの人のための、<豆> + <コンストラクタ、引数> ノードコンストラクタ注入
クラス人
パッケージconstxiong.interview.inject;
publicクラス人{
プライベートボウルボウル;
人(ボウルボウル)公共{
this.bowl =ボウル;
}
公共ボイドは(){EAT
; bowl.putRice()
のSystem.out.println( "...食べ始める");
}
}
スプリングプロファイル
ます。<?xml version = "1.0"エンコード= "UTF-8"? >
<ビーンズのxmlns = " HTTP://www.springframework.org/schema/beans"
のxmlns:XSI =" http://www.w3.org/2001/XMLSchema-instance "
のxmlns:コンテキスト=" http://www.springframework.org/schema/context "
のxsi:schemaLocationの="
のhttp:// www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org /schema/context/spring-context.xsd "> ;
<ビーンID ="ボウル"クラス= "constxiong.interview.inject.Bowl"/>
<ビーンID = "人"クラス=" constxiong.interview.inject.Person ">
<コンストラクタ、引数名="ボウル」REF = "ボウル"> </コンストラクタ、引数>
</豆>
</豆>
C、<ビーン>ノードファクトリメソッドのパラメータは、静的ファクトリメソッド指定
ファクトリクラス、静的ファクトリメソッドを
パッケージconstxiong.interview.inject。
パブリッククラスBowlFactory {
パブリック静的最終ボウルgetBowl(){
)(新しいボウルを返します。
}
}
春配置文件
<xmlのバージョン= "1.0"エンコード= "UTF-8"?>
<豆のxmlns =」http://www.springframework.org/schema/beans "
のxmlns:XSI =" のhttp:// www.w3.org/2001/XMLSchema-instance "
のxmlns:コンテキスト=" http://www.springframework.org/schema/context "
のxsi:schemaLocationの="
http://www.springframework.org/schema/beans
のhttp ://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
ます。http://www.springframework。;
<豆ID = "ボール"クラス= "constxiong.interview.inject.BowlFactory"工場METHOD = "getBowl" />
<ビーンID = "人"クラス= "constxiong.interview.inject.Person">
<constructor-引数名=「ボール」REF =「ボール」> </コンストラクタ、引数>
</ビーン>
</豆>
D、非静的ファクトリメソッド、工場Beanとファクトリメソッド指定
非静的ファクトリメソッド、ファクトリクラスを
パッケージconstxiong。 interview.inject;
publicクラスBowlFactory {
公共ボウルgetBowl(){
)(新しい新しいボウルを返す;
}
}
プロファイル
<XMLバージョン= "1.0"エンコードは= "UTF-8"?>
<ビーンズのxmlnsは= " HTTP:// www.springframework.org/schema/beans "
のxmlns:XSI ="http://www.w3.org/2001/XMLSchema-instance "
のxmlns:コンテキスト="http://www.springframework.org/schema/context "
XSI:のschemaLocation ="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans。 XSD
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd "> ;
<ビーンID =" bowlFactory "クラス=" constxiong.interview。 inject.BowlFactory "> </ビーン>
<ビーンID ="ボウル」工場豆= "bowlFactory"ファクトリメソッド= "getBowl" />
<ビーンID = "人"クラス= "constxiong.interview.inject.Person" >
<コンストラクタ、引数名= "ボウル" REF = "ボウル"> </コンストラクタ、引数>
</豆>
</豆>
2、注釈
ビーン肯定登録
@Componentは//すべての豆登録
ビーン//登録制御層@Controllerを
@Service //登録されたサービス層ビーン
@Repository //登録DAO層ビーン
ビーンに注入
工法に@Autowired行為、一般メンバ変数に使用AxiTraderのリベートwww.fx61.com/brokerlist/axitrader.htmlフィールド、メソッド、上記のフィールド。
@Autowired + @Qualifier注射、豆の指定された名前
@Resource JDK注入自身の注釈、およびBeanタイプの名前を指定することができ
、テストコード
E、スプリングプロファイルは、ディレクトリ設け注釈をスキャン
の<?xml version =「1.0」エンコード= " "> UTF-8?
<ビーンズのxmlns =" http://www.springframework.org/schema/beans "
のxmlns:XSI =" http://www.w3.org/2001/XMLSchema-instance
のxmlns:コンテキスト= " http://www.springframework.org/schema/context "
のxsi:schemaLocationの= "
http://www.springframework.org/schema/beans
HTTP://www.springframework。
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd "> ;
<文脈:コンポーネント・ベース・パッケージスキャン=" constxiong.interview " />
</豆>
クラスボウル
パッケージconstxiong.interview.inject;
インポートorg.springframework.stereotype.Component;
//インポートorg.springframework.stereotype.Controller;
//インポートorg.springframework.stereotype.Repository;
//インポートORG .springframework.stereotype.Serviceは、
@Componentは、すべてのBeanの登録//
// //コントローラ@制御層Beanを登録する
//サービス// @豆登録サービス層
豆// @リポジトリ//登録DAO層
パブリッククラスボウル{
公共空putRice(){
System.out.printlnは( "Shengfan ...");
}
}
Personクラスの
constxiong.interview.injectのためのパッケージ変更;
//インポートjavax.annotation.Resource;
//
インポートorg.springframework.beans.factory.annotation.Autowired;
//インポートorg.springframework.beans.factory.annotation.Qualifier、
インポートORG .springframework.stereotype.Componentは、
@Componentは、すべてのBeanの登録//
// //コントローラ@制御層Beanを登録する
//サービス// @豆登録サービス層
豆// @リポジトリ//登録DAO層
パブリッククラスPersonを{ BR /> Autowired @です
// @Qualifier( "ボウル")
// @Resource(名前= "ボウル")
プライベートボウルボウル;
公共ボイド(){EAT
bowl.putRiceを();
System.out.printlnは(「開始;)」...食べる
}
}
上記試験クラス
A、B、C、D、 Eの試験結果OK
Shengfan ...
私が食べ始めました...

おすすめ

転載: blog.51cto.com/14511863/2438676