简体   繁体   中英

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?

Have you tried something like:

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

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

The aspect is unrelated to your annotation, but I understand you tried the simplest pointcut first.

As your around advice returns void , it can only intercept methods also returning void . Probably you are trying to intercept methods with other return types, though, I can only speculate because you did not share your target code. Make sure the around advice returns Object (or whichever return type your target methods have) and the result of proceed() is returned by the advice.

BTW, your decision not to use Spring just because of its "AOP lite" framework Spring AOP was a good one, AspectJ is much more powerful and efficient. It would be crazy to use Spring just for its limited AOP capabilities. I really don't know why people think about Spring AOP first when it comes to AOP in Java, not about AspectJ.

At some point the aspect needs to be "weaved" into the classes so they can run. This can be done at runtime or at compile time.

I would in fact suggest you use compile time as I think it significantly simplifies using aspectj (I've used compile time quite successfully for a long time now).

You're just going to need to find and configure the gradle plugin for this (I use maven).

请分享用于配置方面的gradle插件

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM