AOPを実現するために注釈を使用Spring-(IX)

メモは、AOPのコードの流れが非常に簡単になり得るが、我々は原則が何であるかであることを理解する必要があります。

当社独自のAOPの実装では、いくつかのメモを追加することで達成することができます

注意点:

コメント欄に書く - >アスペクト

出発点は、その上に強化プラス対応する音符に直接書き込むことができます。

注釈を追加すると、自動エージェント識別コードをプロファイル----> [<AOP:AspectJの-自動プロキシ/>]

 

対象読者変わらずUserServiceのとuserServiceImpl

パッケージorg.west.anno; 

        インポートorg.aspectj.lang.annotation.After;
         インポートorg.aspectj.lang.annotation.Aspect;
         インポートorg.aspectj.lang.annotation.Before; 

// セクション注釈
@Aspect
 パブリック クラス庵野{
     //    エントリポイントは、上記強化直接書き込むことができ 
    @Before( "実行(org.west.service.UserServiceImpl *を。*(..))" 公共 のボイドの前に(){ 
        System.out.printlnは(「---- " - >メソッドが実行される前に、)

    } 


    (@After実行(org.west.service.UserServiceImpl * *(..)。)" " 公共 無効(後){
        (のSystem.out.println "------>メソッド実行後" ); 
    } 

}

右、はいAspectJのパッケージを案内するパッケージの前と後の注意事項に注意してください。

Springのコア構成ファイル

<?xml version = "1.0"エンコード= "UTF-8"?> 
<豆のxmlns = "http://www.springframework.org/schema/beans" 
       のxmlns:XSI = "http://www.w3.org / 2001 / XMLスキーマ・インスタンス" 
       のxmlns:AOP = "http://www.springframework.org/schema/aop" 

       のxsi:schemaLocationの =" http://www.springframework.org/schema/beans 
        のhttp:// WWW。 springframework.org/schema/beans/spring-beans.xsd 
        ます。http:// www.springframework.org/schema/aop 
         ます。http:// www.springframework.org/schema/aop/spring-aop.xsd "> 
<! - -注册豆对象- > 
    <ビーンID = "UserServiceの"クラス = "org.west.service.UserServiceImpl" /> 
<! -注釈AOPのクラスを達成- > 
    <豆上記のid =「安野」を述べましたクラス = "org.west.anno.Anno" /> 
< -自動エージェント識別注釈- >!
     <AOP:AspectJの-自動プロキシ/> 

    
</豆>

テストカテゴリ:

パブリック クラスTestDemo { 
    @Test 
    公共 ボイド試験(){ 

        ApplicationContextのコンテキスト = 新しい ClassPathXmlApplicationContext( "applicationContext2.xml" )。

        UserServiceのUserServiceの =(UserServiceの)context.getBean( "UserServiceの" )。

        userService.add(); 

    }

AOPの概要:

  • 自然は、動的プロキシです

  • aspectjweaver:パッケージAOPに織り込まするパケットに必要な

  • カットを欠場しないように注意してください。

  • AOPを実現するには、3つの方法

    • AOPを実現するためにSpringAPIを使用して、

    • AOPを実装するカスタムクラスを使用します

    • AOPを実現するために注釈を使用します

 

おすすめ

転載: www.cnblogs.com/xiaoqiqistudy/p/11305378.html