gpt4 book ai didi

java - 使用aspectJ的AOP中的Joinpoint VS ProceedingJoinPoint?

转载 作者:IT老高 更新时间:2023-10-28 13:52:30 26 4
gpt4 key购买 nike

谁能告诉我JoinpointProceedingjoinpoint有什么区别?

切面类的方法中什么时候使用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();
}
}

ProceedingJoinPoint 与 'JointPoint 相比有何不同?还有pjp.proceed()`会为我们做什么?

最佳答案

环绕通知是一种特殊的通知,可以控制何时以及是否执行方法(或其他连接点)。这仅适用于环绕通知,因此它们需要类型为 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 的确切方式例如,项目有效。

环绕通知的另一个特点是它们必须有一个返回值,而其他通知类型不能有一个。

关于java - 使用aspectJ的AOP中的Joinpoint VS ProceedingJoinPoint?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15781322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com