What is Spring AOP?

Zoltán Raffai
4 min readOct 25, 2018

--

Do you know about what is Spring AOP? Do you use it in your current Spring boot projects? If not, this is for you.

Nowadays I see a lot of projects which use the latest technologies. They usually made in Spring boot or other cutting-edge frameworks. On one hand, it is promising but on the other hand, I often see that most of the times they do not take the best advantages of that specific technology.

I think it is because most of the books/documentation are too academic, and you can easily miss its understanding. That is why I decided to talk a bit about Aspect Oriented Programming. I would like to make sure that you will not miss it in your next Spring boot project.

What is Aspect Oriented Programming(AOP)?

Aspect-Oriented programming is a programming paradigm which tries to solve the problems of cross-cutting concerns. Aspect-oriented Programming (AOP) complements Object-oriented Programming (OOP) by providing another way of thinking about program structure.

The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect.

In a more simpler word, it helps us to refactor the different necessary repeating codes into different modules. Which gives us the benefit that we can maintain these functionalities in one single place, instead of it writing down every time.

This approach will result in a much more maintainable code, which clears the business logic from the most confusion factor. We separate these functionalities along different aspects.

An aspect is a modularization of a concern that cuts across multiple classes. Unified logging or transaction management can be a good example of it.

Simplify code using AOP

Let us see the given code example below.

Here you can recognize a couple of different concerns, which are not related to the business logic itself. We should separate these into another place. Henceforward we only the business logic has left.

How AOP works on a large scale

If you have a system that contains several packages and classes and you do not use AOP that aspects such as Tracing, Transactions, and Exception Handling, have to implement in every class and every method.

This results in two problems:

  • Code Tangling: Each class, each method contains Tracing, Transactions, and Exception Handling, and also Business Logic. In a tangled code it is often hard to see what is actually going on in a method.
  • Code Scattering: Aspects such as Transactions are scattered throughout the code and not implemented in a single specific part of the system.

Using AOP, allows you to solve these problems. So what AOP does is it takes all the Transaction code, and puts it into a Transaction Aspect, then it takes all the Tracing Code and puts that into an Aspect and finally, all the Exception Handling is also put into an Aspect.

Afterward, there will be a clean separation between the Business Logic and all those additional aspects.

Cross-Cutting Concerns

An important concept in AOP is the Cross-Cutting concerns. Above, I did show a few examples of Tracing, Exception Handling, and Transactions are cross-cutting concerns. Several class and methods must implement them.

In classic object-oriented programing, they cannot implement in a single place so you will not be able to avoid scattering and code tangling.

Aspect-Oriented programming allows you to implement those cross-cutting concerns in one centralized place. So if you use AOP, the way it works is that you implement your business logic first, then you ride aspects for your cross-cutting concerns.

That way you can compose your infrastructure, so you can choose whether and where you would like to add transactions or logging to your code. Therefore, you can create your own individual customized middleware. Then you use Spring AOP to add your aspects to your application, so that at runtime the aspects, as well as your main line business logic is actually executed.

What is Spring AOP?

If you check out my article about the Spring framework, you can see that it is one the core building module of Spring.

It supports us to achieve Aspect-Oriented modularization in our projects, by eliminating code tangling and cross-cutting concerns. Adding the Spring AOP library to our project, you can start to get the advantages of several different tools which it contains.

For eg.: Annotations, Advice, Joinpoints, Pointcuts. These are the most essential foundations of the library.

Usually, you use AOP to implement enterprise features that make the Spring framework so useful.

AOP allows you to define exactly where you would like to integrate the specific aspects that you defined. Hence you will get a configurable middleware. If you would like to know more about what is Spring AOP wait for upcoming articles or visit the official site.

Summary

In this article a tried to clarify the concept of the Aspect-Oriented programming and introduce you how it relates to the Spring framework. In my upcoming articles, I will go deeper into Spring AOP library. What are the main cornerstones of it, how you can use it? Hope I could help, see you soon and do not forget to share. :)

--

--

Zoltán Raffai
Zoltán Raffai

Written by Zoltán Raffai

Author of https://www.zoltanraffai.com/blog/ - Senior software engineer | Team lead | Java Spring

No responses yet