15

I am using aspectj to intercept methods that are annotated with @Profile(description="something")

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Profile {
    public String description() default "";
}

@Around("com.merc.aop.ctw.aspect.PointcutDefinitions.logAnnotatedMethods(profile)")
public Object profile(ProceedingJoinPoint pjp, Profile profile) throws Throwable {
    ....
}

@Pointcut("@annotation(com.merc.annotations.Profile)")
protected void logAnnotatedMethods(Profile profile) {
}

But I get the following error msg while compileing using AJC

formal unbound in pointcut 
Radek Postołowicz
  • 4,506
  • 2
  • 30
  • 47
user373201
  • 10,945
  • 34
  • 112
  • 168
  • Hi, my requirement is same like yours. I have one doubt what is 'com.merc.aop.ctw.aspect.PointcutDefinitions.logAnnotatedMethods'. I notice that logAnnotatedMethods you have created but I am not getting what is com.merc.aop.ctw.aspect.PointcutDefinitions ? Please guide me. – Jimmy Jul 07 '15 at 18:19

2 Answers2

21
@Pointcut("@annotation(com.merc.annotations.Profile)")
protected void logAnnotatedMethods(Profile profile) {
}

This is not correct, @annotation() wants a parameter name, not a parameter type.

If your class is compiled with debug code, the pointcut parameter must have the same name as the method parameter, if not, you need to either rely on the parameter types being unique or explicitly write out your parameter names using the argNames parameter:

@Pointcut(value="@annotation(profile)",argNames="profile")
protected void logAnnotatedMethods(Profile arg) {    }

Reference:

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 2
    I disagree. Just used the @annotation with a annotation type as the parameter and it works like a charm in Spring 3.1 just as the docs say it should. http://static.springsource.org/spring/docs/3.0.3.RELEASE/spring-framework-reference/html/aop.html – Joseph Lust Mar 12 '13 at 13:45
  • 1
    Actually, the docs specify both versions. You'll find my version in this section: http://static.springsource.org/spring/docs/3.0.3.RELEASE/spring-framework-reference/html/aop.html#aop-ataspectj-advice-params . I was, however, not aware of the other version. – Sean Patrick Floyd Mar 12 '13 at 13:54
  • 2
    But then: this is an AspectJ question, not a Spring AOP question, so the Spring docs are hardly relevant. But I took this from the Book AspectJ in Action: `@annotation(TypePattern or ObjectIdentifier)` which again shows that we are both right, just as in Spring AOP – Sean Patrick Floyd Mar 12 '13 at 13:57
8

I was playing around and found that the following worked

@Pointcut("@annotation(profile)")
protected void logAnnotatedMethods(Profile profile) {
}
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
user373201
  • 10,945
  • 34
  • 112
  • 168