Spring Aspect Oriented Programming

The explanation in Wikipedia for this is very very good. https://en.wikipedia.org/wiki/Aspect-oriented_programming

In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding behavior to existing code (an advice) without modifying the code itself, instead separately specifying which code is modified via a pointcut specification, such as log all function calls when the function's name begins with 'set'. This allows behaviors that are not central to the business logic (such as logging) to be added to a program without cluttering the code core to the functionality.

AOP includes programming methods and tools that support the modularization of concerns at the level of the source code, while aspect-oriented software development refers to a whole engineering discipline.

What are some examples of cross cutting concerns?

Logging exemplifies a crosscutting concern because a logging strategy necessarily affects every logged part of the system. Logging thereby crosscuts all logged classes and methods.

  • Wikipedia

https://en.wikipedia.org/wiki/Aspect-oriented_programming#Terminology

Terminology

Cross-cutting concerns

Even though most classes in an OO model will perform a single, specific function, they often share common, secondary requirements with other classes. For example, we may want to add logging to classes within the data-access layer and also to classes in the UI layer whenever a thread enters or exits a method. Further concerns can be related to security such as access control[8] or information flow control.[9] Even though each class has a very different primary functionality, the code needed to perform the secondary functionality is often identical.

Cross-cutting concerns are parts of a program that rely on or must affect many other parts of the system. This is like injecting complete module(logging, cahcing, etc ) in Spring framework dynamically.

Logging, caching, security, monitoring, etc. are some of the examples cross cutting concern from AOP. At any point of time there modules can be added dynamically.

Advice

This is the additional code that you want to apply to your existing model. In our example, this is the logging code that we want to apply whenever the thread enters or exits a method.

Pointcut

This is the term given to the point of execution in the application at which cross-cutting concern needs to be applied. In our example, a pointcut is reached when the thread enters a method, and another pointcut is reached when the thread exits the method.

Aspect

The combination of the pointcut and the advice is termed an aspect. In the example above, we add a logging aspect to our application by defining a pointcut and giving the correct advice.

Practical examples

Using AOP for logging errors in a specific format to make things easier for Splunk alerts

Put this in a common library that is used by your applications and it will be applicable to all the classes that match the package name.

@Slf4j
@Aspect
@Component
public class ExceptionLoggingAspect {
    @AfterThrowing(pointcut = "within(com.company..*)", throwing = "t")
    public void logAfterThrowingAllMethods(Throwable t) {
        if (t instanceof ValidationException ||
                t instanceof ValidationException ||
                t instanceof AnyOtherKnownExceptionForWhichYouDontWantToUseTheAspect) {
            return;
        }
        log.error(String.format("ERR500: %s", t.getMessage()), t);
    }
}

Implementation example

https://github.com/explorer436/programming-playground/tree/main/java-playground/spring-aspect-oriented-programming-demo

Reading material

https://www.baeldung.com/spring-aop


Links to this note