詳細スプリング(C) - AOP

AOP:ノートは完了するために

1、依存jarファイルを追加します

2、クラスのセクションを作成します

パッケージcom.zhiyou.zjc.aspect。

輸入java.util.Arrays。

輸入org.aspectj.lang.JoinPoint。
輸入org.aspectj.lang.annotation.After。
輸入org.aspectj.lang.annotation.Aspect;
輸入org.aspectj.lang.annotation.Before;
輸入org.springframework.stereotype.Component。
輸入org.springframework.stereotype.Controller。@Component 
@Aspect


パブリック クラスCountAspect {     
    @Before( "実行(* com.zhiyou.zjc.aspect.CountDaoImp。*(..))" 公共 ボイドlogbefore(ジョインポイントジョインポイント){ 
        オブジェクト[]引数 = joinPoint.getArgs()。
        System.out.println( "==========前==========" + joinPoint.getSignature()のgetName()+。は、Arrays.asList(引数)); 
    } 
    @After( "実行(* com.zhiyou.zjc.aspect.CountDaoImp。*(..))" 公共 ボイドlogafter(){ 
        System.out.printlnは(「=====後===== ==」); 
    } 
    
}

 

3、春のコンフィギュレーション・ファイルノートに切り開い

< コンテキスト:コンポーネント・スキャンベースパッケージ= "com.zhiyou.zjc.aspect" > </ コンテキスト:コンポーネント・スキャン> 
< AOP:AspectJの-自動プロキシ/>

 

4、試験

パッケージcom.zhiyou.zjc.aspect。

輸入org.springframework.context.ApplicationContext;
輸入org.springframework.context.support.ClassPathXmlApplicationContext; 

パブリック クラスのテスト{
     公共 静的 ボイドメイン(文字列[]引数){ 
        ApplicationContextのアプリ = 新しい ClassPathXmlApplicationContext(「spring.xml」)。
        CountDao coImp =(CountDao)app.getBean( "IMP2" )。
        coImp.add( 2、3 )。
    }   
    
}

 

AOP:XMLベースの方法

<?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" 
    の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/contextのhttp://www.springframework .ORG /スキーマ/コンテキスト/スプリングコンテキスト4.2.xsd 
        http://www.springframework.org/schema/aopのhttp://www.springframework .ORG /スキーマ/ AOP /春-AOP-4.2.xsd」> 
    <! - 定义被通知的程序类- >
     = "IMP2" クラス= "com.zhiyou.zjc.aspect.CountDaoImp2" > </ > 
    <! - 豆クラス定義のセクション- > 
    < ID = "アスペクト" クラス=「com.zhiyou。 zjc.aspect.CountAspect2 "  /> 
    
    <! - XMLファイルの構成セクション- > 
    < AOP:設定> 
        <! - カスタム式カットオフポイント- > 
        < AOP:ポイントカット表現= "実行(* com.zhiyou.zjc.aspect * *(..)。。)" ID = "ポイントカット" />     
        <! - 定义切面- >     
        < AOP:縦横ID = "lodaspect" REF =」側面" >
            <! - 定义前置通知- > 
            < AOP:前メソッド= "logbefore" ポイントカット-REF = "ポイントカット" /> 
            <! - 定义后置通知- > 
            < AOP:後のメソッド= "logafter" ポイントカット-REF = "ポイントカット" /> 
        </ AOP:態様> 
        
        
    </ AOP:設定> 
</ >

 

おすすめ

転載: www.cnblogs.com/zjc364259451/p/11503356.html