java Joinpoint VS ProceedingJoinPoint in AOP using aspectJ?

o0lyfsai  于 12个月前  发布在  Java
关注(0)|答案(4)|浏览(90)

有谁能告诉我JoinpointProceedingjoinpoint有什么区别吗?
什么时候在aspect类的方法中使用JoinpointProceedingjoinpoint
我在AspectJ类中使用了JoinPoint,如下所示:

@Pointcut("execution(* com.pointel.aop.test1.AopTest.beforeAspect(..))")  
public void adviceChild(){}  

@Before("adviceChild()")  
public void beforeAdvicing(JoinPoint joinPoint /*,ProceedingJoinPoint pjp - used refer book marks of AOP*/){ 

    //Used to get the parameters of the method !
    Object[] arguments = joinPoint.getArgs();
    for (Object object : arguments) {
        System.out.println("List of parameters : " + object);
    }

    System.out.println("Method name : " + joinPoint.getSignature().getName());
    log.info("beforeAdvicing...........****************...........");
    log.info("Method name : " + joinPoint.getSignature().getName());
    System.out.println("************************"); 
}

但我在其他资源中看到的是:

@Around("execution(* com.mumz.test.spring.aop.BookShelf.addBook(..))")
public void aroundAddAdvice(ProceedingJoinPoint pjp){
    Object[] arguments = pjp.getArgs();
    for (Object object : arguments) {
        System.out.println("Book being added is : " + object);
    }
    try {
        pjp.proceed();
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

在这里,与'JointPoint ? Also what will pjp.proceed()相比,ProceedingJoinPoint`有什么不同?

pw136qt2

pw136qt21#

around通知是一种特殊的通知,可以控制何时以及是否执行方法(或其他连接点)。这只适用于around建议,所以它们需要一个ProceedingJoinPoint类型的参数,而其他建议只使用普通的JoinPoint。一个示例用例是缓存返回值:

private SomeCache cache;

@Around("some.signature.pattern.*(*)")
public Object cacheMethodReturn(ProceedingJoinPoint pjp){
    Object cached = cache.get(pjp.getArgs());
    if(cached != null) return cached; // method is never executed at all
    else{
        Object result = pjp.proceed();
        cache.put(pjp.getArgs(), result);
        return result;
    }
}

在这段代码中(使用一个不存在的缓存技术来说明一点),只有当该高速缓存不返回结果时,才会调用实际的方法。例如,这就是Spring EHCache Annotations项目的工作方式。
around advances的另一个特点是它们必须有一个返回值,而其他advice类型必须没有。

f3temu5u

f3temu5u2#

@Around("execution(* com.mumz.test.spring.aop.BookShelf.addBook(..))")

这意味着在调用com.mumz.test.spring.aop.BookShelf.addBook方法之前调用aroundAddAdvice方法。System.out.println("Book being added is : " + object);操作完成后。它将调用实际方法addBook()pjp.proceed()将调用addBook()方法。

t1rydlwq

t1rydlwq3#

使用JoinPoint,建议类型如下:

@Before, @After, @AfterReturning, @AfterThrowing

使用ProceedingJoinPoint,建议类型如下:

@Around
ajsxfq5m

ajsxfq5m4#

ProceedingJoinPoint是一个具有额外功能的JoinPoint
ProceedingJoinPoint@Around建议一起使用。@Around是一个非常强大的建议,它结合了建议的其他功能。
ProceedingJoinPoint::proceed基本上用于执行原始方法。
考虑以下示例:

@Around("@annotation(com.annotations.ExecutionLoggerAround)")
public void executeLogger(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    System.out.println("Method execution starts at :: " + System.currentTimeMillis());
    proceedingJoinPoint.proceed();
    System.out.println("Method execution completes at :: " + System.currentTimeMillis());
}

@ExecutionLoggerAround
public void generateReport() {
    System.out.println("Generating XLS report !!");
}

public @interface ExecutionLogger {
}

现在,当您调用generateReport时,将执行Around建议。如果跳过proceedingJoinPoint.proceed()行,实际的方法将不会执行。您将在控制台中只看到这两个println
PS:为了更好地理解,你需要知道AOP是如何工作的。Spring使用JDK代理或CGLIB代理创建代理对象。所以它实际上是在运行时执行的代理对象,它在后台使用我们的方法。
我写了更多关于这个here

相关问题