スプリング(3)

 

 

パッケージcom.cn.pojo。

輸入org.springframework.stereotype.Component; 

@Component 
パブリック クラス予約{ 

}
パッケージcom.cn.dao。

輸入org.springframework.context.annotation.Scope;
輸入org.springframework.stereotype.Repository。

@Repository 
@Scope( "プロトタイプ" パブリック クラスBookDao { 

}
パッケージcom.cn.dao。

輸入org.springframework.stereotype.Repository。

@Repository 
パブリック クラス BookExtは延びBookDaoを{ 

}
パッケージcom.cn.controller。

輸入org.springframework.stereotype.Controller; 

@Controller 
パブリック クラスBookController { 

}
パッケージcom.cn.service; 

インポートorg.springframework.beans.factory.annotation.Autowired;
 インポートorg.springframework.beans.factory.annotation.Qualifier;
 インポートorg.springframework.stereotype.Service; 

インポートcom.cn.dao.BookDao ; 

@Service 
パブリック クラスBookServiceを{ 
    
/ ** 
     *自動的に注入@Autowired 
     * 1、および射出プレスAの種類見つける。
     見つかった場合、id属性名が継続することにより以下のように、* 2、複数個見つけると注入
     *を。3、複数のを発見した場合。しかし、id属性名が見つからなかったとして、@Qualifier(「bookDao」)注釈が見つけて注入するためにIDを指定します使用することができる
     *を。4、(必要= false)を@Autowiredを変更することができ、フィールドの値がnullであることができます
     * / 
    @ Autowired(必須 =  
    修飾子@(「bookDaoExt」プライベート; BookDao bookDao1 
    
    / ** 
     オブジェクトが作成された後*メソッドにマーク@Autowired、このメソッドが呼び出されます。
     * 1、プレスルックアップパラメータタイプおよびコール転送するための方法
     のIDが検索および注入を続けるようにパラメータ名が続く* 2を、複数見つかった場合、
     * 3、複数見つかった場合。しかし、idパラメータ名が見つからないとして、あなたは@Qualifierを使用することができます(「bookDao」)注釈が見つけて呼び出すためのIDを指定します
     * 4を、あなたは@Autowired(必要= false)を変更することができますが、このメソッドを呼び出すことが許可されていない与えられていません
     * / 
    @Autowired(必要 = falseに公共 無効 setBookDao(@Qualifier( "bookExt" )BookDao ABC){ 
        System.out.printlnは( + ">>> BookDaoはラ---来る" ABC)を、
    

    文字列のtoString(){
         リターン "BookServiceを[bookDao =" + bookDao1 + "]" 
    } 
}
パッケージcom.cn.test; 

輸入 静的 org.junit.Assert *。; 

輸入org.junit.Test;
輸入org.springframework.beans.factory.annotation.Configurable;
輸入org.springframework.context.ApplicationContext;
輸入org.springframework.context.ConfigurableApplicationContext;
輸入org.springframework.context.support.ClassPathXmlApplicationContext; 

パブリック クラスSpringTest { 
    @Test 
    公共 ボイド TEST1()はスロー例外{ 

        のApplicationContextのApplicationContext = 新規のClassPathXmlApplicationContext( "applicationContext.xmlを" );
        / * 
         *のSystem.out.println(applicationContext.getBean( "bookDao")); 
         *のSystem.out.println(applicationContext.getBean( "bookDao")); 
         *のSystem.out.println(applicationContext.getBean( "bookDao")); 
         * 
         * 
         *のSystem.out.println(applicationContext.getBean( "本")); 
         * / 
        System.out.printlnは(applicationContext.getBean( "BookServiceを" )); 

    } 

}
<?XMLバージョン= "1.0"エンコード= "UTF-8" ?> 
< ビーンズのxmlns = "http://www.springframework.org/schema/beans" 
    のxmlns:XSI =「http://www.w3.org / 2001 / XMLスキーマ・インスタンス" 
    の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-4.3.xsd " > 


    < ! - そのサブパケットがスキャンされます)を使用-デフォルト・フィルタ=を提供するスキャンするパッケージ名スキャンベースパッケージ構成バンドル「偽」
        パッケージを削除する場合は、デフォルトのルールのスキャンが含まれています- > 
    <コンテキスト:スキャンコンポーネントベース・パッケージ=「com.cn」   > 

    - <!      に従って注釈を除外
        :<コンテキスト除外フィルタ型=「注釈」
            表現=「org.springframework.stereotype.Service」/> 
        タイプ別の負負

        <コンテキスト:フィルタタイプ= "ASSIGNABLE"除外
            表現= "com.cn.controller.BookController" /> 
 - > 


<! - 含む注釈に係る- > 
        <! - <文脈:含めるフィルタ型= "注釈" 
            表現=" org.springframework.stereotype.Service「/> 
 - > 

<! - 構成するには、クラスを除外します
  - > 
        <! - <コンテキスト:インクルードフィルタタイプ=「アサイン」
            表現= "com.cn.controller.BookController" /> 
 - >
    </ コンテキスト:コンポーネントスキャン> 
</ >

 

おすすめ

転載: www.cnblogs.com/ywqtro/p/12263398.html