Spring AOP - pointcut where method has a custom annotation
Jul 2, 2014
This is a quick example for how to run a method when any invoked method has a particular annotation. In this example, we will log the time it takes to execute any method adorned with a @LogDuration annotation. (Why log duration? You may wish to do something else. This is fine - just do that instead.)
###1. Tell spring to use AOP and annotations.
In this case, we’re using <aop:aspectj-autoproxy>.
###2. Create your desired annotation
###3. Write your AOP logic
In this case, we should choose the @Around annotation. This is because we need to do something before AND after the method invocation.
###4. Put your annotation on a method.
In this case, I put @LogDuration on an API method in a Spring MVC application.