【发布时间】:2019-05-29 09:21:42
【问题描述】:
是否可以使用 AspectJ 围绕最终方法添加建议?我完全知道使用 Spring AOP 是不可能的。但我找不到任何与 AspectJ 相关的内容。
【问题讨论】:
标签: spring-boot aspectj spring-aop final
是否可以使用 AspectJ 围绕最终方法添加建议?我完全知道使用 Spring AOP 是不可能的。但我找不到任何与 AspectJ 相关的内容。
【问题讨论】:
标签: spring-boot aspectj spring-aop final
对于介绍性的个人意见,我很抱歉,但我想说的是:您想使用 AspectJ。 您为什么不尝试一下? 与在这里写一个问题并等待有人回答相比,您获得结果要快得多。您的问题格式也不适合 SO,因为您没有发布任何遇到问题的代码,您只是提出一般性问题。
是的,使用 AspectJ 可以检测最终类和/或方法:
带有 final 方法的驱动应用程序:
package de.scrum_master.app;
public class Application {
public final void doSomething() {}
public static void main(String[] args) {
new Application().doSomething();
}
}
原生 AspectJ 语法中的方面:
package de.scrum_master.aspect;
public aspect MyAspect {
Object around() : execution(* doSomething()) {
System.out.println(thisJoinPoint);
return proceed();
}
}
@AspectJ 语法中的切面:
我真的更喜欢原生语法,但无论如何,出于某种未知原因,有些人似乎更喜欢这种带有导入、抛出、显式声明的连接点实例和更复杂的处理方式的丑陋而冗长的版本:
package de.scrum_master.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class MyAspect {
@Around("execution(* doSomething())")
public Object aroundAdvice(ProceedingJoinPoint thisJoinPoint) throws Throwable {
System.out.println(thisJoinPoint);
return thisJoinPoint.proceed();
}
}
控制台日志:
两种方面变体的效果相同:
execution(void de.scrum_master.app.Application.doSomething())
【讨论】: