10

以下は私のポイントカットとアドバイスの宣言です

//PointCut on A method which takes two parameters and is in a DAO
@Pointcut("execution(backend.repository.QuestionsRepository.AnswerQuestion (..))")
public void answerQuestionPointCut() {}


@Around(
   value="web.activity.advisors.UserActivityAdvisor.answerQuestionPointCut()",
   argNames="question,answer"
)

 // Do something

}

次のエラーが発生します

Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting 'name pattern' at character position 65
execution(backend.repository.QuestionsRepository.AnswerQuestion (..))
                                                                 ^

これで立ち往生している、任意のポインタ

4

3 に答える 3

19

戻り値の型がありません:

@Pointcut("execution(* backend.repository.QuestionsRepository.AnswerQuestion (..))")

次のように、引数名をバインドする必要があります。

@Pointcut("execution(* backend.repository.QuestionsRepository.AnswerQuestion (..)) 
&& args(question, answer)") // wrapped for readability only

ソリューション例

サービス インターフェイス:

package foo.bar.service;
public interface Service{
    void whack(String thing, Integer thang);
}

実装クラス:

package foo.bar.service;
public class DefaultService implements Service{
    @Override
    public void whack(final String thing, final Integer thang){
        System.out.println(
            "Inside thing: " + thing + ", inside thang: " + thang
        );
    }
}

春の AOP の側面:

@Aspect
public class ServiceAspect{

    @Pointcut("execution(* foo.bar.service.*.*(..))")
    public void serviceMethodExecution(){
    }

    @Around(value = "serviceMethodExecution() && args(param1, param2)")
    public void aroundServiceMethodExecution(final ProceedingJoinPoint pjp,
        final String param1,
        final Integer param2) throws Throwable{

        System.out.println("Before thing: " + param1 + ", before thang: "
            + param2);
        pjp.proceed();
        System.out.println("After thing: " + param1 + ", after thang: "
            + param2);
    }

}

スプリング コンテキスト XML:

<aop:aspectj-autoproxy proxy-target-class="false"  />
<bean class="foo.bar.service.DefaultService" />
<bean class="foo.bar.aspects.ServiceAspect" />

テスト用のメイン クラス:

次に、プロセス全体をテストするための主な方法を示します。サービス Bean とアスペクトを定義する上記の XML を使用して、 XML 構成なしで Spring ApplicationContext を開始します (AspectJ ウィービングをオンにしたため、XML を使用しないソリューションのみが機能することがわかりました。どの Bean を含める必要があるかわかりません)。アスペクト j-autoproxy を有効にするのでClassPathXmlApplicationContext、この最小限の XML を使用します):

public static void main(final String[] args){
    final ApplicationContext applicationContext =
        new ClassPathXmlApplicationContext("classpath:/aspectContext.xml");
    final Service service = applicationContext.getBean(Service.class);
    service.whack("abc", 123);
}

出力:

Before thing: abc, before thang: 123
Inside thing: abc, inside thang: 123
After thing: abc, after thang: 123

これで始められるはずです。基本的に:JDKプロキシ(春のデフォルト)を使用する場合、インターセプトしているメソッドがサービスインターフェースによってサポートされていることを確認する必要があります。Spring AOP プロキシ メカニズムについては、こちらを参照してください。

ノート:

ご覧のとおり、メソッドの引数をポイントカットではなくアスペクトにバインドしているため、異なる引数シグネチャを持つメソッドでポイントカットを再利用できるようになっています。ただし、次のように、ポイントカットでそれらをバインドすることもできます。

@Pointcut(value = "execution(* foo.bar.service.*.*(..)) && args(a,b)",
          argNames = "a,b")
public void serviceMethodExecution(final String a, final Integer b){
}

@Around(value = "serviceMethodExecution(param1, param2)",
        argNames = "param1,param2")
public void aroundServiceMethodExecution(final String param1,
    final Integer param2,
    final ProceedingJoinPoint pjp) throws Throwable{

    System.out.println("Before thing: " + param1 + ", before thang: "
        + param2);
    pjp.proceed();
    System.out.println("After thing: " + param1 + ", after thang: "
        + param2);
}
于 2010-09-30T15:55:49.573 に答える
2

あなたはこのように書くべきです

@Pointcut("execution(* backend.repository.QuestionsRepository.AnswerQuestion (..))")

注意を払う"... (* backend..."

* スペースを使用する必要があります

于 2012-04-01T14:38:54.397 に答える
1

@Beforeの注釈と同様の動作をすることに注意してくださいorg.aspectj.lang.annotation.Before

実行キーワードなしで戻り値の型なしで式を使用できます。

@Before("allGetters()")

または両方で:

@Before("execution(public * allGetters())")

executionただし、戻り値の型を使用せずにキーワードを使用することはできません。

于 2016-06-06T14:48:55.343 に答える