0
0

More than 1 year has passed since last update.

【Java】【Spring】Pointcut is not well-formed: expecting 'name pattern' エラーの解決法

Posted at

はじめに

今回はPointcut is not well-formed: expecting 'name pattern'というエラーにハマったので、メモしておきます。

エラーコード

package com.example.demo.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Aspect
@Component
@Slf4j
public class LogAspect {

    /**
     * サービスの実行前にログ出力する
     * 対象:[UserService]をクラス名に含んでいる
     */
    @Before("execution(**..*.*UserService.*(..))")
    public void startLog(JoinPoint jp) {
        log.info("メソッド開始:" + jp.getSignature());
    }

    /**
     * サービスの実行後にログを出力する
     * 対象:[UserService]をクラス名に含んでいるs
     */
    @After("execution(**..*.*UserService.*(..))")
    public void endLog(JoinPoint jp) {
        log.info("メソッド終了:" + jp.getSignature());
    }

}

エラーが出たのはPoiontcut部分の正規表現の部分だが、パット見間違ってなさそうに思えます。

結論

ただのケアレスミスというのが結論です。
というのも、正規表現の先頭部分のアスタリスク間に空白がなかったというだけでした。

なので、正しいコードとしては以下のようになります。
(※アノテーション部分だけ記載)

@Before("execution(* *..*.*UserService.*(..))")

@After("execution(* *..*.*UserService.*(..))")

これだけでエラーは解決しました。
プログラミングにはあるあるですが、スペルミス、半角・全角スペースなどエラーの原因の8割はケアレスミスです。

ただ自分では見つけにくいのが難点ですが、、、

executionの構文

"execution(<戻り値> 空白 <パッケージ名>.<クラス名>.<メソッド名>(<引数>))"

今回使用したPointcutのexecutionの構文は上記の構文なので、ケアレスミスを起こさないようにするためにも、ルールに従って記述することが重要です。

参考文献

【Java】【Spring】Pointcut is not well-formed: expecting 'name pattern' エラーの対処法

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0