Spring AOP


Introduction

  • 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.
  • AOP is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding additional behavior to existing code without modification of the code itself.
  • Instead, we can declare this new code and these new behaviors separately.
  • Some of the common crosscutting concerns are logging, transaction management, data validation, security, etc.
  • Spring AOP module provides interceptors to intercept an application. For example, when a method is executed, you can add extra functionality before or after the method execution.

Key Concepts

  • Aspect
    • Modularization of a concern that cuts across multiple classes
    • @Aspect annotation or using schema-based XML
  • Joinpoint
    • A join point is a specific point in the application such as method execution, exception handling, changing object variable values, etc. In Spring AOP a join point is always the execution of a method.
  • Advice
    • Advices are methods that get executed when a certain join point with a matching pointcut is reached in the application.
    • Many AOP frameworks, including Spring, model advice as an interceptor, maintaining a chain of interceptors around the join point.
    • Types of advice
      • Before advice @Before
        • Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).
      • After returning advice @AfterReturning
        • Advice to be executed after a join point completes normally: for example if a method returns without throwing an exception.
      • After throwing advice @AfterThrowing
        • Advice to be executed if a method exists by throwing an exception.
      • After (finally) advice @After
        • Advice that gets executed after the join point method finishes executing, whether normally or by throwing an exception.
      • Around advice @Around
        • Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.
  • Pointcut
    • A regular expression that matches join points. An advice is associated with a pointcut expression and runs at any join point that matches the pointcut
    • Supported Pointcut Designators – execution, args, within, annotation, this, target
    • Spring AOP users are likely to use the execution pointcut designator the most often. The syntax of an execution expression is: –

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)

Pointcut Examples

  • The execution of any public method
execution(public * *(..))
  • The execution of any method with a name beginning with “set“
execution(* set*(..))
  • The execution of any method defined by the AccountService interface
execution(* com.xyz.service.AccountService.*(..))
  • Any join point (method execution) where the target object has an @Transactional annotation
@annotation(org.springframework.transaction.annotation.Transactional)
  • Any join point (method execution only in Spring AOP) which takes a single parameter, and where the argument passed at runtime is Serializable
args(java.io.Serializable)

AOP Other Concepts

  • Target Object
    • They are the object to which advices are applied. Spring AOP is implemented using runtime proxies so this object is always a proxied object. What is means is that a subclass is created at runtime where the target method is overridden, and advice are included based on their configuration.
  • AOP Proxy
    • Spring AOP implementation uses JDK dynamic proxy to create the Proxy classes with target classes and advice invocations, these are called AOP proxy classes. We can also use CGLIB proxy by adding it as the dependency in the Spring AOP project.
  • Weaving
    • linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.
  • Introduction
    • declaring additional methods or fields on behalf of a type. Spring AOP allows you to introduce new interfaces (and a corresponding implementation) to any advised object. For example, you could use an introduction to make a bean implement an IsModified interface, to simplify caching. (An introduction is known as an inter-type declaration in the AspectJ community.)

Reference

https://docs.spring.io/spring-framework/docs/6.0.x/reference/html/core.html#aop

https://docs.spring.io/spring-framework/docs/6.0.x/reference/html/core.html#aop-api