Spring AOP

Spring AOP (Aspect-Oriented Programming) is a powerful feature of the Spring framework that allows you to add additional behavior to your application at runtime. This cheatsheet provides an overview of some of the most commonly used concepts and annotations in Spring AOP.

Key Concepts

Aspect

An aspect is a modularization of a concern that cuts across multiple objects. In Spring AOP, an aspect is implemented as a Java class that defines pointcut(s) and advice(s) that are applied to a target object.

Join Point

A join point is a point in the execution of a program, such as the invocation of a method or the handling of an exception. In Spring AOP, join points are identified by pointcut expressions.

Advice

An advice is the action taken by an aspect at a particular join point. There are several types of advice, including:

  • Before advice: executed before a join point

  • After returning advice: executed after a join point completes normally

  • After throwing advice: executed after a join point throws an exception

  • After advice: executed after a join point completes, regardless of its outcome

  • Around advice: wraps a join point, allowing the advice to control its execution

Pointcut

A pointcut is a predicate that matches one or more join points. Pointcuts can be defined using various expressions, including method names, annotations, and regular expressions.

Weaving

Weaving is the process of applying aspects to a target object to create a new, woven object. In Spring AOP, weaving can be performed at compile time or at runtime.

Annotations

AnnotationDescription

@Aspect

Identifies a class as an aspect

@Pointcut

Defines a pointcut expression to identify join points

@Before

Indicates advice to be executed before a join point

@After

Indicates advice to be executed after a join point

@AfterReturning

Indicates advice to be executed after a join point completes normally

@AfterThrowing

Indicates advice to be executed when a join point throws an exception

@Around

Indicates advice to be executed around a join point

@DeclareParents

Introduces interfaces to a target class

@Order

Specifies the order of advice execution

@EnableAspectJAutoProxy

Enables Spring's aspect auto-proxying capability

@AspectJBasedPointcutAdvisor

Enables the use of an AspectJ pointcut expression with Spring AOP

Example

@Aspect
@Component
public class LoggingAspect {

    @Before("execution(public * com.example.myapp.*Controller.*(..))")
    public void logBefore(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("Entering " + methodName);
    }

    @AfterReturning(pointcut = "execution(public * com.example.myapp.*Controller.*(..))",
        returning = "result")
    public void logAfterReturning(JoinPoint joinPoint, Object result) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("Exiting " + methodName + " with result " + result);
    }

    @AfterThrowing(pointcut = "execution(public * com.example.myapp.*Controller.*(..))",
        throwing = "exception")
    public void logAfterThrowing(JoinPoint joinPoint, Exception exception) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("Exception thrown in " + methodName + " with message " + exception.getMessage());
    }
}

In this example, the LoggingAspect class is marked as an aspect, and three advice methods are defined using the @Before, @AfterReturning, and @AfterThrowing annotations. The pointcut expression selects all public methods in classes with names ending in "Controller" in the com.example.myapp package. When one of these methods is executed, the appropriate advice method is called.

Last updated