Spring AOP切面日志Demo 配置文件方式與注解方式


一、配置文件方式

1、配置applicationContext.xml,

<bean id="logAopBean" class="com.demo.common.aop.LogAop"></bean>
    <aop:config>
        <aop:aspect id="logAspect" ref="logAopBean">
            <aop:pointcut expression="execution(* com.demo..*(..))" id="allMethod"/>
            <aop:before method="before" pointcut-ref="allMethod" />
            <aop:after-throwing method="afterThrowing" pointcut-ref="allMethod" />
            <aop:after-returning method="afterReturn" pointcut-ref="allMethod" />
            <aop:after method="after" pointcut-ref="allMethod" />
        </aop:aspect>
    </aop:config>

2、日志處理類,

/**
 * LogAop.java
 * 
 * Shanghai NTT DATA Synergy Software Co., Ltd. All Rights Reserved.
 * @author wyl
 * @date 2016-10-18
 */ 

package com.demo.common.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * @author wyl
 * @Description TODO 
 * @date 2016-10-18
 *
 */

public class LogAop {
    public void before(JoinPoint call){
        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
        System.out.println("開始執行:"+className+"."+methodName+"()方法...");
    }
    public void afterThrowing(JoinPoint call){
        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
        System.out.println(className+"."+methodName+"()方法拋出了異常...");
    }
    public void afterReturn(JoinPoint call){
        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
        System.out.println(className+"."+methodName+"()方法正常執行結束...");
    }
    public void after(JoinPoint call){
        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
        System.out.println(className+"."+methodName+"()最終執行步驟(finally)...");
    }
    /*//用來做環繞通知的方法可以第一個參數定義為org.aspectj.lang.ProceedingJoinPoint類型  
    public Object doAround(ProceedingJoinPoint call) throws Throwable {  
        Object result = null;  
        this.before(call);//相當於前置通知  
        try {  
            result = call.proceed();  
            this.afterReturn(call); //相當於后置通知  
        } catch (Throwable e) {  
            this.afterThrowing(call); //相當於異常拋出后通知  
            throw e;
        }finally{  
            this.after(call);  //相當於最終通知  
        }  
        return result;  
    }*/
}

二、注解方式

1、配置applicationContext.xml,

<bean id="logAspectBean" class="com.demo.common.aop.LogAnnotationAspect"></bean>
    <aop:aspectj-autoproxy/>

2、日志處理類,

/**
 * LogAnnotationAspect.java
 * 
 * Shanghai NTT DATA Synergy Software Co., Ltd. All Rights Reserved.
 * @author wyl
 * @date 2016-10-18
 */ 

package com.demo.common.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/**
 * @author wyl
 * @Description TODO 
 * @date 2016-10-18
 *
 */

@Aspect  //定義切面類  
public class LogAnnotationAspect {  
    @SuppressWarnings("unused")  
    //定義切入點,提供一個方法,這個方法的名字就是改切入點的id  
    @Pointcut("execution(* com.demo..*(..))")  
    private void allMethod(){}  
    //針對指定的切入點表達式選擇的切入點應用前置通知  
    @Before("allMethod()")    
    public void before(JoinPoint call) {  
        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
        System.out.println("開始執行:"+className+"."+methodName+"()方法...");
    }  
    //訪問命名切入點來應用后置通知  
    @AfterReturning("allMethod()")  
    public void afterReturn(JoinPoint call) {  
        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
        System.out.println(className+"."+methodName+"()方法正常執行結束...");
    }  
    //應用最終通知  
    @After("allMethod()")  
    public void after(JoinPoint call) {  
        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
        System.out.println(className+"."+methodName+"()最終執行步驟(finally)...");
    }  
    //應用異常拋出后通知  
    @AfterThrowing("allMethod()")  
    public void afterThrowing(JoinPoint call) {  
        String className = call.getTarget().getClass().getName();
        String methodName = call.getSignature().getName();
        System.out.println(className+"."+methodName+"()方法拋出了異常...");
    }  
    //應用周圍通知  
    //@Around("allMethod()")  
    public Object doAround(ProceedingJoinPoint call) throws Throwable{  
        Object result = null;  
        this.before(call);//相當於前置通知  
        try {  
            result = call.proceed();  
            this.afterReturn(call); //相當於后置通知  
        } catch (Throwable e) {  
            this.afterThrowing(call);  //相當於異常拋出后通知  
            throw e;  
        }finally{  
            this.after(call);  //相當於最終通知  
        }  
        return result;  
    }  
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM