我有两个方面,每个方面都修改方法参数。当两个方面都应用于同一个方法时,我希望这些方面的执行被链接起来,并且我希望在第一个方面修改的参数可以通过第二个方面使用joinPoint.getArgs();
但是,似乎每个方面只获得原始论点;第二个方面永远不会看到修改后的值。我设计了一个例子:
测试类:
public class AspectTest extends TestCase {
@Moo
private void foo(String boo, String foo) {
System.out.println(boo + foo);
}
public void testAspect() {
foo("You should", " never see this");
}
}
foo() 方法有两个方面的建议:
@Aspect
public class MooImpl {
@Pointcut("execution(@Moo * *(..))")
public void methodPointcut() {}
@Around("methodPointcut()")
public Object afterMethodInControllerClass(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("MooImpl is being called");
Object[] args = joinPoint.getArgs();
args[0] = "don't";
return joinPoint.proceed(args);
}
}
和...
@Aspect
public class DoubleMooImpl {
@Pointcut("execution(@Moo * *(..))")
public void methodPointcut() {}
@Around("methodPointcut()")
public Object afterMethodInControllerClass(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("DoubleMooImpl is being called");
Object[] args = joinPoint.getArgs();
args[1] = " run and hide";
return joinPoint.proceed(args);
}
}
我希望输出是:
MooImpl is being called
DoubleMooImpl is being called
don't run and hide
...但是:
MooImpl is being called
DoubleMooImpl is being called
You should run and hide
我是否使用正确的方法通过周围的建议修改参数?