Spring AOP
June 8, 2020

M02 Q01 What is the concept of AOP?

AOPAspect Oriented Programming – a programming paradigm that complements Object-oriented Programming (OOP) by providing a way to separate groups of cross-cutting concerns from business logic code.

It is important to remember that AOP is not something that should be used instead of OOP. We should use AOP together with OOP. And when we are facing with that OOP with design patterns, SOLID concepts and such far cannot give us a proper solution to the problem, then we should look at the AOP.

It is also important not to overuse AOP because we might end up with the code that is much harder to read, maintain and debug than if you would only write pure OOP code.

AOP allows us to implement cross-cutting concerns. This is achieved by the ability to add additional behaviour to the code without having to modify the code itself. This is achieved by specifying:

  • Pointcut is matched with Join point - location of the code which behaviour should be altered
  • Advice - code which should be executed that implements cross-cutting concern

Let's look at the two examples of code. First will be without using AOP. Second with using AOP paradigm.

Without the usage of AOP

We have an application that invokes ComplexReportAction

ComplexReportService has few method invocations. First one is fetching report. The second - formatting the report. The third - save the report. And the last one is to fetch report again.

You can see that between major methods I invoke logger methods too. This code is not readable. Logger methods duplicated.

'This is the issue that you will have if you are implementing that is called a cross-cutting concern but you are not using AOP.

With the usage of AOP

Now let's look at how we can do this better with the usage of AOP.

Below there is the same class but now this code is much easier to read. It is because all of the cross-cutting concerns are implemented via aspects.

All of the code related to the performance execution is in Aspect.

This class is called an aspect. Aspect contains advice - code that is being injected. This advice saves start time and calculates the duration of the execution and then it logs the execution on the screen.

Also, it contains a Point cut expression which is matched with Join point. Join point is a method execution.

Point cut expression

Point cut expression is the way for AOP to know when to inject this code. And this point cut means that we want to execute aspect for methods with annotation @PerformanceLogger

Now let's look at the caching. I have created @Cachable annotation and CachableAspect.

The goal of this aspect is to execute whenever we have a join point matched with the point cut expression. And this point cut expression says that it will be executed for methods with @Cachable annotation.

The aspect will check if the result of the method is already in the cache. If it is already in the cache then it will fetch the value from the cache. If it is not in the cache it will execute the method and it will remember the result of the method in the cache. So next time when it will be executed it will not execute the real method again.

Which problem does it solve?

Aspect-Oriented Programming solves the following challenges:

  • Allows proper implementation of Cross-Cutting Concerns
  • Solves Code Duplications by eliminating the need to repeat the code for functionalities across different layers, such functionalities may include logging, performance logging, monitoring, transactions, caching
  • Avoids mixing unrelated code, for example mixing transaction logic code (commit, rollback) with business code makes code harder to read, by separating concerns code is easier to read, interpret, maintain

What is a cross-cutting concern?

Common cross-cutting concerns:

  • Logging
  • Performance Logging
  • Caching
  • Security
  • Transactions
  • Monitoring

What two problems arise if you don't solve a cross-cutting concern via AOP?

Implementing cross-cutting concerns without using AOP, produces the following challenges:

  • Code duplications – Before/After code duplicated in all locations when normally Advise would be applied, refactoring by extraction helps but does not fully solve the problem
  • Mixing of concerns – business logic code mixed with logging, transactions, caching makes code hard read and maintain