3

重複の可能性:
特定のアノテーションを持つクラスのすべてのメソッドに対する @AspectJ ポイントカット

カスタム注釈を持つクラスのすべてのメソッドのポイントカットを作成しようとしています。これがコードです

  • 注釈:

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Retention(value = RetentionPolicy.RUNTIME)
    @Target(value = ElementType.METHOD)
    public @interface ValidateRequest {}
    
  • コントローラーのメソッド:

     @RequestMapping(value = "getAbyB", produces = "application/json")
     @ResponseBody
     @ValidateRequest
     public Object getAbyB(@RequestBody GetAbyBRequest req) throws Exception { 
         /// logic
     }
    
  • 側面:

     @Aspect
     @Component
     public class CustomAspectHandler {
         @Before("execution(public * *(..))")
         public void foo(JoinPoint joinPoint) throws Throwable {
             LOG.info("yippee!");
         }
     }
    
  • アプリケーションコンテキスト:

    `<aop:aspectj-autoproxy />`
    

以下に示すさまざまなアドバイスを試してきましたが、どれもうまくいかないようです(上記で使用したものを除く)

  • @Around("@within(com.pack.Anno1) || @annotation(com.pack.Anno1)")
  • @Around("execution(@com.pack.Anno1 * *(..))")
4

1 に答える 1

2

これはうまくいくはずです:

 @Aspect
 @Component
 public class CustomAspectHandler {
    @Pointcut("execution(@ValidateRequest * *.*(..))")
    public void validateRequestTargets(){}

     @Around("validateRequestTargets()")
     public Object foo(JoinPoint joinPoint) throws Throwable {
         LOG.info("yippee!");
         return joinPoint.proceed();
     }
 }
于 2012-12-27T16:29:46.353 に答える