0

I have defined a custom annotation as below.

package com.xyz;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Loggable {
    String message() default "Log Message";
}

My aspect class contains the below method:

@Around(value = "@annotation(com.xyz.Loggable)")
public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
    // come code here
}

My service interface is as below.

public interface Service {
   @Loggable
   public void method1();
}

My implementation is as below.

public class ServiceImpl implements Service {
     public void method1() {
         // some code here
     }
}

With this setup, My advice is not getting triggered. (however it gets triggered if i move the @Loggable annotation to method1() in ServiceImpl class).

I would like to keep the annotation defined at interface level instead of method implementations. Is there a way to get this work ?

opuser1
  • 427
  • 7
  • 29

1 Answers1

0

No, that is not possible (yet?).

Annotations can only be inherited among Classes and even then only if they are themselves annotated with the meta-Annotation @Inherited:

http://docs.oracle.com/javase/8/docs/api/java/lang/annotation/Inherited.html

It is not possible to have annotations on Interfaces be inherited to their implementing classes.

This is also explained in the AspectJ documentation: http://www.eclipse.org/aspectj/doc/released/adk15notebook/annotations.html#annotation-inheritance

@Inherited annotations are not inherited when used to annotate anything other than a type. A type that implements one or more interfaces never inherits any annotations from the interfaces it implements.

sheltem
  • 3,754
  • 1
  • 34
  • 39