2

非常に基本的なspectJプロジェクトを作成しました。アドバイスを適用できない理由がわかりません。

注釈

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)  
@Target(ElementType.METHOD)  
@Documented  
public @interface Conditional {  
    String value();  
}  

そして、aspectJ クラス:

@Aspect
public class AspectE {

    @Around("call(@Conditional * *.*(..)) && @annotation(conditional)" )
    public Object condition(ProceedingJoinPoint joinPoint, Conditional conditional) throws Throwable {

        System.out.println("entry point says hello!");
        return joinPoint.proceed();
    }
}

主要:

public class Main {

    @Conditional("")
    public static void main(String[] args) {
        System.out.println("main good morning");
    }
}

両方のメッセージを受信するには何を変更すればよいか教えてください。

4

3 に答える 3

2

call(@Conditional * *.*(..))基本的に callers を織り込んでいるためだと思います。この特定のケースの呼び出し元はコマンドラインであるため、織り込みは行われません。

おそらく代わりに実行に変更する必要があります。それは機能するはずです。

@Around("execution(@Conditional * *.*(..)) && @annotation(conditional)" )

于 2012-08-21T12:03:01.893 に答える
0

You should use AspectJ weaver in order to apply your aspect AspectE to the Java class Main. If you're using Maven I would recommend to use aspectj-maven-plugin.

于 2012-08-21T11:39:32.797 に答える
0
  1. あなたのアドバイスはproceed(conditional)、オブジェクトを変数にバインドする必要がない場合は、バインドを削除する必要があります。

  2. ポイントカット マッチングの問題は、アプリケーション内に呼び出す場所がなくmain(Java コマンド ラインがそれを行う)、メソッドが実行される場所しかないため、ポイントカットのその部分を に変更する必要があることですexecution(@Conditional * *.*(..))

  3. 簡単にするために、ポイントカットのいずれかの部分を削除できます&&

したがって、プレーンな AspectJ 構文でのアスペクトの最も単純なバージョンは次のとおりです。

public aspect AspectE {
    Object around() : @annotation(Conditional) {
        System.out.println("entry point says hello!");
        return proceed();
    }
}
于 2012-08-21T12:17:55.723 に答える