春AOP練習の手

applicationContext.xmlを:

<?XMLバージョン= "1.0"エンコード= "UTF-8" ?> 
< のxmlns:XSI = "http://www.w3.org/2001/XMLSchema-instance" 
       のxmlns = "http://www.springframework.org /スキーマ/豆"  
       のxmlns:コンテキスト= "http://www.springframework.org/schema/context"  
       のxmlns:AOP = "http://www.springframework.org/schema/aop" のxsi:schemaLocationの="のhttp: //www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/contextのhttp:// WWW .springframework.org /スキーマ/コンテキスト/春-コンテキスト4.2.xsd http://www.springframework.org/schema/aopのhttp://www.springframework。
< コンテキスト:コンポーネント・スキャンベースパッケージ= "com.kong.bean" > </ コンテキスト:コンポーネント・スキャン> 
< ビーン= "myPerform" クラス= "com.kong.service.MyPerform" > </ ビーン> 
< 豆の名前= "myAdvice" クラス= "com.kong.service.MyAdvice" > </ > 
< AOP:設定> 
    < AOP:アスペクト   REF = "myAdvice" > 
    < AOP:ポイントカット表現=「実行(* com.kong .service.MyPerform。持続(..))」 ID = "peisistCut"/> 
        < AOP:前ポイントカット-REF = "peisistCut" メソッド= "doBefore" /> 
        < AOP:後のポイントカット-REF = "peisistCut" メソッド= "doAfterRegardlessIsOk" /> 
        < AOP:アフター復帰ポイントカット-REF = "peisistCut " メソッド=" doAfter」/> 
        < AOP:アフター投げポイントカット-REF = "peisistCut" メソッド= "doAfterThrowing" /> 
        < AOP:周りポイントカット-REF = "peisistCut" メソッド= "doSurround"/> 
    </ AOP:側面> 
</AOP:設定> 
</ >

カットPointクラス:MyPerform.java

パッケージcom.kong.service。

パブリック クラス{MyPerform
     公共 ボイド(){持続
        するSystem.out.println( "やる持続()メソッドを!!!" ); 
    } 
    
    公共 ボイドは(){削除
        するSystem.out.println( "行う削除()メソッドを!!!" )。
    } 
    
    公共 ボイドは(){修正
        するSystem.out.println( "行う変更()メソッドを!!!" )。
    } 
    
    公共 ボイド検索(){ 
        System.out.printlnは( "見つけるか()メソッドを!!!" )。
    } 

}

セクション:

パッケージcom.kong.service。

輸入org.aspectj.lang.ProceedingJoinPoint。

パブリック クラスMyAdvice {
     公共 ボイドdoBefore(){ 
        System.out.printlnは( "myAdvice_method:doBefore()!" )。
    } 
    公共 ボイドdoAfter(){ 
        System.out.printlnは( "myAdvice_method:doAfter()!" )。
    } 
    公共 ボイドdoAfterRegardlessIsOk(){ 
        System.out.printlnは( "myAdvice_method:doAfterRegardlessIsOk()!" )。
    } 
    パブリック 無効 doAfterThrowing(){
        System.out.println( "myAdvice_method:doAfterThrowing;プログラムが例外を来る場合、このメッセージが表示されます!!!" ); 
    } 
    
    パブリック 無効 doSurround(ProceedingJoinPointのPJ)がスローInterruptedExceptionあるが、{
         しようと{ 
            (System.out.printlnは "hehehhe、方法doSurround()が始まります!" ); 
            System.out.println( "駆動時間niddleを始めます" ); 
            ロングスタート = にSystem.currentTimeMillis(); 
            Thread.sleep( 2000 ); 
            pj.proceed(); 
            長い端 = にSystem.currentTimeMillis();
            System.out.println(「方法doSurround()端、使用時間『+(エンドスタート)+』ミル」)。
        } キャッチ(ThrowableをE){
             // TODO自動生成されたcatchブロック 
            のSystem.out.println( "例外が発生しました" )。
            e.printStackTrace(); 
        } 
        
    } 

}

テスト:

パッケージcom.kong.test。
輸入org.junit.Test;
輸入org.springframework.context.ApplicationContext;
輸入org.springframework.context.support.ClassPathXmlApplicationContext; 輸入com.kong.service.MyPerform。
パブリック クラスのMyTest {
 
    @Test 
    公共  ボイドtest_01(){ 
    
         ApplicationContextのAC = 新しい ClassPathXmlApplicationContext( "applicationContext.xmlを" )。
         MyPerform myPerform =(MyPerform)ac.getBean( "myPerform" )。
         myPerform.persist(); 
    } 
    
}

結果はドロップです。^ _ ^。

ます。https://www.cnblogs.com/kongieg/p/11051177.htmlで再現

おすすめ

転載: blog.csdn.net/weixin_34104341/article/details/93693518