關於Aop切面中的@Before @Around等操做順序的說明

【轉】http://www.cnblogs.com/softidea/p/6123307.html html

話很少說,直接上代碼:java

package com.cdms.aop.aspectImpl;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 建立 by 草帽boy on 2017/4/1.
 */
@Aspect
@Component
public class ICacheAopAction {
    @Pointcut("@annotation(com.cdms.aop.ICache)")
    private void controllerAspect(){}

    @Before("controllerAspect()")
    public void Before(JoinPoint joinPoint){
        String classname = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("@before Execute! --class name: " + classname + ", method name: " + methodName + " " + args );
    }

    @Around("controllerAspect()")
    public Object Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("@Around:執行目標方法以前...");
        Object obj= proceedingJoinPoint.proceed();
        System.out.println("@Around:執行目標方法以後...");
        System.out.println("@Around:被織入的目標對象爲:" + proceedingJoinPoint.getTarget());
        System.out.println( "@Around:原返回值:" + obj + ",這是返回結果的後綴");
        return obj;
    }

    @AfterThrowing("controllerAspect()")
    public void AfterThrowing(){
        System.out.println("異常通知....");
    }

    @After("controllerAspect()")
    public void After(JoinPoint point){
        System.out.println("@After:模擬釋放資源...");
        System.out.println("@After:目標方法爲:" +
                point.getSignature().getDeclaringTypeName() +
                "." + point.getSignature().getName());
        System.out.println("@After:參數爲:" + Arrays.toString(point.getArgs()));
        System.out.println("@After:被織入的目標對象爲:" + point.getTarget());
    }

    @AfterReturning("controllerAspect()")
    public void AfterReturning(JoinPoint point){
        System.out.println("@AfterReturning:模擬日誌記錄功能...");
        System.out.println("@AfterReturning:目標方法爲:" +
                point.getSignature().getDeclaringTypeName() +
                "." + point.getSignature().getName());
        System.out.println("@AfterReturning:參數爲:" +
                Arrays.toString(point.getArgs()));
        System.out.println("@AfterReturning:返回值爲:" );
        System.out.println("@AfterReturning:被織入的目標對象爲:" + point.getTarget());
    }

}

  測試的結果是:spring

這樣就很清楚的看出各類方法是在何時調用的啦ide

相關文章
相關標籤/搜索