在AspectJ中捕获通知执行(advising advice)
Capturing advice execution (advising advice) in AspectJ
我试图在 Maven 中使用注释捕获建议的执行,但它说该建议尚未应用。这是代码:
package testMaven8;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
@Aspect
public class aspecter {
public static int a;
public void setA(int a) {
this.a = a;
}
public int getA() {
return a;
}
@Before("execution(* testMaven8.das.qwe(..))")
public void testBefore2(){
System.out.println("yoo2");
setA(102);
System.out.println(a);
}
@Before("execution (* testMaven8.aspecter.testBefore2(..))")
public void advice1(){
System.out.println("advicing advice testBefore2");
}
}
代码有问题吗?如果可能的话,我试图避免使用 Aspect class。任何帮助表示赞赏。
谢谢
通知执行在 AspectJ 中是一种特殊的东西,而不是正常的方法执行。因此,如果你想捕获通知执行,你还需要一个具有令人惊讶的名称的特殊切入点 - adviceexecution()
;-)
下面是一些示例代码:
驱动申请:
package de.scrum_master.app;
public class Application {
public static void main(String[] args) {
new Application().doSomething();
}
public void doSomething() {
System.out.println("Doing something");
}
}
方面捕获方法执行:
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class MethodInterceptor {
@Before("execution(!static * *(..))")
public void interceptMethod(JoinPoint thisJoinPoint) {
System.out.println(thisJoinPoint);
}
}
方面捕获建议执行:
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AdviceInterceptor {
@Before("!within(AdviceInterceptor) && adviceexecution()")
public void interceptAdvice(JoinPoint thisJoinPoint) {
System.out.println(thisJoinPoint);
}
}
请不要尝试将通知执行拦截器打包到与另一个通知相同的方面,因为如果您不能排除 adviceexecution()
切入点通过 [=16 捕获自身,您很容易陷入无休止的递归=].
控制台日志:
adviceexecution(void de.scrum_master.aspect.MethodInterceptor.interceptMethod(JoinPoint))
execution(void de.scrum_master.app.Application.doSomething())
Doing something
我试图在 Maven 中使用注释捕获建议的执行,但它说该建议尚未应用。这是代码:
package testMaven8;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
@Aspect
public class aspecter {
public static int a;
public void setA(int a) {
this.a = a;
}
public int getA() {
return a;
}
@Before("execution(* testMaven8.das.qwe(..))")
public void testBefore2(){
System.out.println("yoo2");
setA(102);
System.out.println(a);
}
@Before("execution (* testMaven8.aspecter.testBefore2(..))")
public void advice1(){
System.out.println("advicing advice testBefore2");
}
}
代码有问题吗?如果可能的话,我试图避免使用 Aspect class。任何帮助表示赞赏。
谢谢
通知执行在 AspectJ 中是一种特殊的东西,而不是正常的方法执行。因此,如果你想捕获通知执行,你还需要一个具有令人惊讶的名称的特殊切入点 - adviceexecution()
;-)
下面是一些示例代码:
驱动申请:
package de.scrum_master.app;
public class Application {
public static void main(String[] args) {
new Application().doSomething();
}
public void doSomething() {
System.out.println("Doing something");
}
}
方面捕获方法执行:
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class MethodInterceptor {
@Before("execution(!static * *(..))")
public void interceptMethod(JoinPoint thisJoinPoint) {
System.out.println(thisJoinPoint);
}
}
方面捕获建议执行:
package de.scrum_master.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AdviceInterceptor {
@Before("!within(AdviceInterceptor) && adviceexecution()")
public void interceptAdvice(JoinPoint thisJoinPoint) {
System.out.println(thisJoinPoint);
}
}
请不要尝试将通知执行拦截器打包到与另一个通知相同的方面,因为如果您不能排除 adviceexecution()
切入点通过 [=16 捕获自身,您很容易陷入无休止的递归=].
控制台日志:
adviceexecution(void de.scrum_master.aspect.MethodInterceptor.interceptMethod(JoinPoint))
execution(void de.scrum_master.app.Application.doSomething())
Doing something