Benefiting from Spring AOP support of @AspectJ annotations is easier than you think.
If you have decided that you need to use the @AspectJ @Around annotation in your Spring Boot web application, you might find that there is a lot of conflicting information about using AspectJ on the Internet. A quick web search for @AspectJ @Around will return many links to articles, tutorials, and stack overflow questions dating back to 2010, and the information referenced does little to acknowledge how to do this in Spring Boot.
Here are a few references that should take precedence over anything you might read on the web:
You will have no doubt read about aspects and join points and pointcuts, and probably a lot more about AspectJ weaving vs Spring CGLIB Proxies and how to configure one or the other in Spring. It turns out that Aspect Oriented Programming is a very deep subject and that Java and Spring are a great place to make use of these concepts.
For now, I’m just going to show you how to use the @Around annotation from @AspectJ in your Spring Boot web application. It really is quite easy.
A Simple Spring Boot Web Application
Let’s start with the simplest Spring Boot web application I could write:
Building and Running the Example Application
This will build and run any Spring Boot application:
Though building and running a Spring Boot application using Maven is childsplay, the verbosity of your Spring Boot applications startup logging can be temporarily eliminated like this:
You can use the java command and add the --logging.level.org=WARN parameter to get rid of the logging output from all the org packages (mostly org.springframework and org.apache).
Once the ExampleApplication is up and running, try hitting the test service:
Your console may look something like this:
Add This Maven Dependency for Spring AOP Support
The only dependency you need to add to your pom.xml for Spring AOP is this:
This is important to know. Don’t be tempted by tutorials to add other things.
Create the Annotation
Every good aspect needs an annotation interface.
Although declaring methods is not a requirement, it provides us with a mechanism for parameterizing the aspect. In this example, we are declaring a msg parameter that will be logged when the annotated method successfully returns.
Create the Aspect
This is the aspect class which effectively intercepts calls to the method where our @Example annotation is used:
I tried to keep it simple. This basically logs the join point signature of the annotated method before calling it. Upon returning (without a Throwable) our msg parameter is logged. Note how Example is automatically passed as a parameter to the executeAround() method for us to use.
Enable AspectJ AutoProxy
You’ll need to enable AspectJ AutoProxy using the @EnableAspectJAutoProxy annotation:
Just add it to the application class where @SpringBootApplication is usually found.
Using the Annotation
This part couldn’t be easier. Just use the @Example annotation on any Spring service method, such as our request mapped test method from ExampleController.java:
Here the Example annotation interface provides the value for msg that is logged upon successful completion of Example.test():
Information About Join Points
You can also see from the preceding output that the method boolean com.example.ExampleController.test() is the target method of our aspect. We can get this information via the ProceedingJoinPoint argument passed to our aspect.
Convenient Way To Add a Filter
It’s important to know that you can intercept a method’s passed arguments, return result, and any thrown exceptions from this vantage point. Our example aspect simply logs and re-throws any exceptions it catches.
Some potential uses:
benchmarking annotated methods
logging method entry/exit
This Just Scratches the Surface of AOP
Just using the @Around annotation in Spring doesn’t make you an AspectJ programmer, and you may never need to fully understand the benefits of AOP to take advantage of this useful and integral functionalty in Spring.