有谁能告诉我Joinpoint
和Proceedingjoinpoint
有什么区别吗?
什么时候在aspect类的方法中使用Joinpoint
和Proceedingjoinpoint
?
我在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`有什么不同?
4条答案
按热度按时间pw136qt21#
around通知是一种特殊的通知,可以控制何时以及是否执行方法(或其他连接点)。这只适用于around建议,所以它们需要一个
ProceedingJoinPoint
类型的参数,而其他建议只使用普通的JoinPoint
。一个示例用例是缓存返回值:在这段代码中(使用一个不存在的缓存技术来说明一点),只有当该高速缓存不返回结果时,才会调用实际的方法。例如,这就是Spring EHCache Annotations项目的工作方式。
around advances的另一个特点是它们必须有一个返回值,而其他advice类型必须没有。
f3temu5u2#
这意味着在调用
com.mumz.test.spring.aop.BookShelf.addBook
方法之前调用aroundAddAdvice
方法。System.out.println("Book being added is : " + object);
操作完成后。它将调用实际方法addBook()
。pjp.proceed()
将调用addBook()
方法。t1rydlwq3#
使用JoinPoint,建议类型如下:
使用ProceedingJoinPoint,建议类型如下:
ajsxfq5m4#
ProceedingJoinPoint
是一个具有额外功能的JoinPoint
。ProceedingJoinPoint
与@Around
建议一起使用。@Around
是一个非常强大的建议,它结合了建议的其他功能。ProceedingJoinPoint::proceed
基本上用于执行原始方法。考虑以下示例:
现在,当您调用
generateReport
时,将执行Around
建议。如果跳过proceedingJoinPoint.proceed()
行,实际的方法将不会执行。您将在控制台中只看到这两个println
。PS:为了更好地理解,你需要知道AOP是如何工作的。Spring使用JDK代理或CGLIB代理创建代理对象。所以它实际上是在运行时执行的代理对象,它在后台使用我们的方法。
我写了更多关于这个here