Learn the Spring AOP, it’s easy and useful.

Rodrigo Puerto Pedrera
4 min readMar 26, 2021

Hello guys, new story new stuff. Today I’m going to take a little tour of Spring AOP or to more distinguished people the “Spring adaptation to Aspect-Oriented Programming Paradigm”.

I discovered it some time ago but I’ve never use it until nowadays. Seriously, this is wonderful and very useful to make a good readable code.

Obviously, I’m not going to explain every concept about the Aspect-Oriented Paradigm, Google have like thousands of articles with them. Here, I’m going to train myself on it by creating a little project, save it for my future self and try to help whoever are you.

Setting up the sample project

First at all, I’m going to create the project for this particular story. You can see the main dependencies that I’m going to add, it’s a really simple project. We also need to add the Aspectj dependency:

You can see all versions in the link pasted, in this project it’ll inherit its version
Spring Initializr for project creation

My package architecture

I’m going to show you how I created the package presentation for this sample project. The very first thing that I saw I could improve in my code was the user authentication.

You can see that I’m going to follow the classic Controller — Service — Persistence architecture, but for timing reasons I’m not going to implement the persistence layer (I’m sorry guys, I’m spaniard).

What am I going to create?

The logic is very simple, the requester will want to get its phone number, because it has never been able to remember it and need to deploy an application into AWS to see its phone number (first world problems).

But he wants first world security, and because of this I implemented the JWT authentication (you know… first world security hehe).

I’ve also added an enum for encryption type (it’s just for me to remember it in future) that will be implemented as an attribute in Authenticated annotation.
This annotation is crucial to creating the look, as it will tell you when it should be invoked (you will see this condition later).

Authenticated annotation for Aspect Advice

You can have all types of decryption you want, and implement them however you want:

Decryption enum ways

Just a little summary

I’m just going to left a simple guide with all the steps you need to set-up the Aspect Processing, just like a cooking recipe.

1. "aspectjweaver" added as a dependency (org.aspectj group)
2. @EnableAspectJAutoProxy annotation in main class.
3. @interface object that will "represent" our aspect.
4. @Aspect & @Component annotations in the class that will have the code related to the aspect

Creating the Aspect

Here, we already have created the custom annotation related to this aspect. We didn’t associated them yet, so how can we make it?

You can see that we are working with Spring Boot annotations, we aren’t making anything with xml files. So an important thing for making everything work, is to add the Component annotation to say to Spring this class is a bean.

Implemented aspect class

This is the very first thing that we can do with an aspect. We’re just creating something that will be invoked before a method is called. As you can see in the Before annotation, I set the condition of: if there is some method marked with the Authenticated annotation, you will be invoked. So right now, we have the @interface class and aspect already associated.

But… I can use this aspect everywhere, it could produce a bug…

Obviously, you can also restrict the use of this aspect to only few packages or methods. You only need to use the next execution condition in Before annotation, just like:

Restricting the use of Authenticated annotation

We aren’t passing a single argument (we can do it if we want), we are only working with annotations. This is the most simple way to start working/enjoying with aspects.

Can I make fantasy out of this?

Definitely yes, you can work with so many advices (how the aspect will be invoked). You can implement the Before advice, After returning advice (it runs when a matched method execution returns normally), After throwing advice (a matched method execution exits by throwing an exception)…

You can also check every parameter that is passed to the annotated method or choose the Aspect instantiation model… There are so many things that will help to you and your team to get a very more readable core, that you will never want to get out of this paradigm.

Do you want more stories related to Aspects?

I think that I will post some more stories related to this world because I’m literally in drugs with it. So if you need help with any type of aspect, leave me a comment and I will post a new story related to it.

So it was a very simple story, I hope I have more time in a future and get more things done. By the way, you can follow me in LinkedIn if you want! https://www.linkedin.com/in/ropuertop/

--

--