Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring AOP: Annotation on any method called x not working

I am getting started with AOP for the first time.

I have my first aspect as follows:

@Aspect
public class SyncLoggingAspect {
    private final Logger logger = Logger.getLogger(this.getClass());

    @Before("execution(public * *(..))")
    public void anyPublic() {
        System.out.println("HIT POINTCUT");
    }
}

This sucessfully proceeds to be invoked on any method call which is public. However when I change it to this:

@Before("execution(public * doPoll(..))")
public void anyPublic() {
    System.out.println("HIT POINTCUT");
}

I would expect it to work on any public method called "doPoll", yet when a method such as this is called nothing happens:

public class GmailContactPoller extends ContactPoller<GoogleUser, ContactPusher<GoogleUser>> {
    Logger logger = Logger.getLogger(this.getClass());

    @Override
    public List<? extends ContactPusher<GoogleUser>> doPoll() throws PollException {
           ...
    }
}

Is there something I am missing with the EL syntax? Or is this something to do with the inheritance hierarchy? The superclass method of doPoll is abstract in an abstract class called Poller. Does not having an interface cause problems?

Edit: I just noticed my IDE enables spring aspect tooling, and now I have the following compiler warning by the method:

"Description Resource Path Location Type advice defined in datasync.aop.aspects.SyncLoggingAspect has not been applied [Xlint:adviceDidNotMatch] SyncLoggingAspect.java /DataSync/src/main/datasync/aop/aspects"

like image 764
mogronalol Avatar asked Feb 03 '12 09:02

mogronalol


People also ask

Does AOP work on static methods?

Yes we can. Because we are accessing the class level members.


1 Answers

Spring AOP Proxies and aspectJ had some differences mainly:

  • Spring AOP only works on public methods.
  • Spring AOP does not work for self invocations.

You can have a look to sections 8.4 & 8.5 of Spring's Documentation for more information.

Currently you have two solutions:

  1. refactor your code to remove the need for self invocation
  2. use AspectJ rather than Spring AOP proxies either at compile time or load time.
like image 60
gabuzo Avatar answered Sep 23 '22 13:09

gabuzo