メソッドの引数を変更する 2 つの側面があります。両方の側面が同じメソッドに適用される場合、側面の実行が連鎖することを期待し、最初の側面で変更された引数が 2 番目の側面で使用できることを期待します。joinPoint.getArgs();
しかし、各側面は元の引数; 2 番目の側面では、変更された値は表示されません。私は例を考案しました:
テストクラス:
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() は、次の 2 つの側面から推奨されます。
@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
アドバイスを介して引数を変更するために正しいアプローチを使用していますか?