Search code examples
model-view-controlleraopaspectj

Spring AOP with aspectj @annotation


I want to apply annotation with aspectJ. (Use Springboot 1.5.1, Mybatis 2.1.1)

So, I made custom annotation and AspectJ.. and apply it.

/** CustomAnnotation */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TestAnnotation {
    String value();
}
/** AspectJ configuration */
@Component
@Aspect
public class AuditTrailAspect {
     @Autowired
     TestDAO dao;
     
    @Around("@annotation(TestAnnotation)") 
    public Object doSomethingAround(ProceedingJoinPoint joinPoint) throws Throwable {
        /* before proceed */
        Object result = joinPoint.proceed();
        /* after proceed */ 
        return result;
    }
}
/** Apply Annoataion at Repository */
@Repository
public interface TestDAO {
    @TestAnnotation(value = "test")
    int insertSomething(RequestDto dto);
}

(this code made simple, for question)

if pointcut expression apply 'execution' this code works fine in Repository(DAO).. also if pointcut expression apply '@annotation' this code works other Component(Service.. Controller)

But, Why can't I apply custom annotation in Repository(DAO) with AspectJ? please help.. Thank you!


Solution

  • Annotations on implemented interfaces cannot be inherited.

    @Inherited causes annotations (only on class) to be inherited from superclasses , but no effect for interface implementations.

    Note that this meta-annotation type has no effect if the annotated type is used to annotate anything other than a class. Note also that this meta-annotation only causes annotations to be inherited from superclasses; annotations on implemented interfaces have no effect.