Search code examples
javagradleaopaspectj

Apply aspect only on annotated methods using AspectJ(without Spring)


I want to use aspects in a Java that has some resources limitations so I can't use Spring aspects due to it's big memory footprint.

So want I want to do without Spring, is to create a custom annotation and which will trigger an aspect on the annotated methods.

I have some implementation but I can't see the Apect being triggered when the method runs. This is what I have:

My custom annotation

 @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyCustomAnnotation {}

Where the annotation is used

@MyCustomAnnotation
     public void someMethod() {
        System.out.println("Method runnning...");
     }

The aspect I created

@Aspect
public class MyAspect {

    @Pointcut("execution(* *(..))")
    public void callAt() {}

    @Around(("callAt()"))
    public void doSomething(ProceedingJoinPoint point)  {
      point.proceed();
      System.out.println("Aspect is runnning...");
}

The configurations I have in my gradle file

dependencies {
        classpath "gradle.plugin.aspectj:gradle-aspectj:0.1.6"
    }

apply plugin: 'aspectj-gradle'

    compile 'org.aspectj:aspectjrt:1.9.1'
    compile 'org.aspectj:aspectjweaver:1.9.1'

I have no idea why the aspect it's not being triggered, I can't see the message from that aspect in my console when the app runs. Any idea what I am missing?


Solution

  • Have you tried something like:

    @Around("@annotation(mycustomannotation.package.MyCustomAnnotation)")
    

    where mycustomannotation.package.MyCustomAnnotation is the interface of the anotation you created