# 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

<table><thead><tr><th width="294">Annotation</th><th>Description</th></tr></thead><tbody><tr><td>@Aspect</td><td>Identifies a class as an aspect</td></tr><tr><td>@Pointcut</td><td>Defines a pointcut expression to identify join points</td></tr><tr><td>@Before</td><td>Indicates advice to be executed before a join point</td></tr><tr><td>@After</td><td>Indicates advice to be executed after a join point</td></tr><tr><td>@AfterReturning</td><td>Indicates advice to be executed after a join point completes normally</td></tr><tr><td>@AfterThrowing</td><td>Indicates advice to be executed when a join point throws an exception</td></tr><tr><td>@Around</td><td>Indicates advice to be executed around a join point</td></tr><tr><td>@DeclareParents</td><td>Introduces interfaces to a target class</td></tr><tr><td>@Order</td><td>Specifies the order of advice execution</td></tr><tr><td>@EnableAspectJAutoProxy</td><td>Enables Spring's aspect auto-proxying capability</td></tr><tr><td>@AspectJBasedPointcutAdvisor</td><td>Enables the use of an AspectJ pointcut expression with Spring AOP</td></tr></tbody></table>

### Example

```java
@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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tingreavinash.gitbook.io/the-tech-toolbox/notes/spring-fundamentals/spring-aop.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
