您需要将切入点限制为调用方法的控制流 - cflow()中的执行,并通过args()绑定调用方法的感兴趣参数。
应用:
package org.group;
import java.util.Arrays;
import java.util.List;
public class Person {
public void method1(String id, String number) {
// some code
List list = getList(number);
// some code
}
public List getList(String number) {
return Arrays.asList(number);
}
public static void main(String[] args) {
// Should not be intercepted
new Person().getList("22");
// Should be intercepted
new Person().method1("John Doe", "11");
}
}
方面:
package de.scrum_master.aspect;
import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class MyAspect {
@Pointcut("execution(* org.group.Person.getList(..))")
public void methodGetList() {}
@Pointcut("execution(* org.group.Person.method1(..)) && args(id, *)")
public void methodMethod1(String id) {}
@AfterReturning(
pointcut = "methodGetList() && cflow(methodMethod1(id))",
returning = "result"
)
public void afterMethodGetList(JoinPoint joinPoint, String id, List result) {
System.out.println(
"I can see the list result " + result +
" for the person with id " + id
);
}
}
控制台日志:
I can see the list result [11] for the person with id John Doe