AspectJ實(shí)現(xiàn)aop

定義Aspect切面,配置對(duì)com.java.gk.springaop.SpringAopDemo的方法進(jìn)行攔截處理

package com.java.gk.springaop;

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.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class SpringAop {
    
    /** 
     * Pointcut 
     * 定義Pointcut,Pointcut的名稱為aspectjMethod(),此方法沒(méi)有返回值和參數(shù) 
     * 該方法就是一個(gè)標(biāo)識(shí),不進(jìn)行調(diào)用 
     */  
//  @Pointcut("execution (* com.java.gk.springaop..*.*(..))")
    @Pointcut("execution (* com.java.gk.springaop.SpringAopDemo.*(..))")
    private void aspectjMethod() {
        
    }
    
    /**  
     * Before 
     * 在核心業(yè)務(wù)執(zhí)行前執(zhí)行,不能阻止核心業(yè)務(wù)的調(diào)用。 
     * @param joinPoint  
     */    
    @Before("aspectjMethod()")
    public void beforeAdvice(JoinPoint joinPoint) {
        System.out.println("-----beforeAdvice().invoke-----");  
        System.out.println(" 此處意在執(zhí)行核心業(yè)務(wù)邏輯前,做一些安全性的判斷等等");
        System.out.println(joinPoint.getTarget().getClass().getName());
        System.out.println(" 可通過(guò)joinPoint來(lái)獲取所需要的內(nèi)容");  
        System.out.println("-----End of beforeAdvice()------");
    }
    
    /**  
     * After  
     * 核心業(yè)務(wù)邏輯退出后(包括正常執(zhí)行結(jié)束和異常退出),執(zhí)行此Advice 
     * @param joinPoint 
     */  
    @After("aspectjMethod()")
    public void afterAdvice(JoinPoint joinPoint) {
        System.out.println("-----afterAdvice().invoke-----");  
        System.out.println(" 此處意在執(zhí)行核心業(yè)務(wù)邏輯之后,做一些日志記錄操作等等");  
        System.out.println(" 可通過(guò)joinPoint來(lái)獲取所需要的內(nèi)容");  
        System.out.println("-----End of afterAdvice()------");  
    }
    
    /**  
     * Around  
     * 手動(dòng)控制調(diào)用核心業(yè)務(wù)邏輯,以及調(diào)用前和調(diào)用后的處理, 
     *  
     * 注意:當(dāng)核心業(yè)務(wù)拋異常后,立即退出,轉(zhuǎn)向AfterAdvice 
     * 執(zhí)行完AfterAdvice,再轉(zhuǎn)到ThrowingAdvice 
     * @param pjp 
     * @return 
     * @throws Throwable 
     */   
    @Around("aspectjMethod()")
    public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("-----aroundAdvice().invoke-----");
        System.out.println(" 此處可以做類似于Before Advice的事情");
        //調(diào)用核心邏輯  
        Object retVal = proceedingJoinPoint.proceed();
        
        System.out.println(" 此處可以做類似于After Advice的事情");
        System.out.println("-----End of aroundAdvice()------");
        return retVal;
    }
    
    /**  
     * AfterReturning  
     * 核心業(yè)務(wù)邏輯調(diào)用正常退出后,不管是否有返回值,正常退出后,均執(zhí)行此Advice 
     * @param joinPoint 
     */   
    @AfterReturning(value = "aspectjMethod()", returning = "retVal")    
    public void afterReturningAdvice(JoinPoint joinPoint, String retVal) {    
        System.out.println("-----afterReturningAdvice().invoke-----");  
        System.out.println("Return Value: " + retVal);   
        System.out.println(" 此處可以對(duì)返回值做進(jìn)一步處理");  
        System.out.println(" 可通過(guò)joinPoint來(lái)獲取所需要的內(nèi)容");  
        System.out.println("-----End of afterReturningAdvice()------");  
    }  
      
    /** 
     * 核心業(yè)務(wù)邏輯調(diào)用異常退出后,執(zhí)行此Advice,處理錯(cuò)誤信息 
     *  
     * 注意:執(zhí)行順序在Around Advice之后 
     * @param joinPoint 
     * @param ex 
     */  
    @AfterThrowing(value = "aspectjMethod()", throwing = "ex")    
    public void afterThrowingAdvice(JoinPoint joinPoint, Exception ex) {    
        System.out.println("-----afterThrowingAdvice().invoke-----");  
        System.out.println(" 錯(cuò)誤信息:"+ex.getMessage());  
        System.out.println(" 此處意在執(zhí)行核心業(yè)務(wù)邏輯出錯(cuò)時(shí),捕獲異常,并可做一些日志記錄操作等等");  
        System.out.println(" 可通過(guò)joinPoint來(lái)獲取所需要的內(nèi)容");  
        System.out.println("-----End of afterThrowingAdvice()------");    
    }    
    
}

需要在spring配置文件中配置bean

<bean id="springAop"  class="com.java.gk.springaop.SpringAop"/>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容