3

Possible Duplicate:
@AspectJ pointcut for all methods of a class with specific annotation

I am trying to write a pointcut for all methods of a class which have a custom annotation. Here's the code

  • Annotation:

    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 {}
    
  • Method in Controller:

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

     @Aspect
     @Component
     public class CustomAspectHandler {
         @Before("execution(public * *(..))")
         public void foo(JoinPoint joinPoint) throws Throwable {
             LOG.info("yippee!");
         }
     }
    
  • applicationContext:

    `<aop:aspectj-autoproxy />`
    

I've been trying various advices, mentioned below, but none seem to work (except for the one used above)

  • @Around("@within(com.pack.Anno1) || @annotation(com.pack.Anno1)")
  • @Around("execution(@com.pack.Anno1 * *(..))")
Community
  • 1
  • 1
Amit Dalal
  • 642
  • 8
  • 20
  • Had been trying different pointcuts for two days and none of them worked. Moved the controller logic as such to service layer and kept custom annotation to service layer only. It worked. Not sure why so.. any clues? – Amit Dalal Dec 29 '12 at 05:45
  • It does't work because does JDK Dynamic Proxies which only intercepts methods defined on an interface. I assume your Controller does not have an interface (normally they don't), so none of the methods on your Controller are intercepted. See http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/aop.html#aop-autoproxy-force-CGLIB – citress Feb 26 '13 at 18:51

1 Answers1

2

This should work:

 @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();
     }
 }
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • Did you try this before posting? I have the similar problem in my case and the aspect is never being executed. I post a new question:http://stackoverflow.com/questions/21279716/error-at-0-cant-find-referenced-pointcut-annotation – ftrujillo Jan 22 '14 at 10:17