CorejavaInterviewBooster15
CorejavaInterviewBooster15
Spring AOP is a key module of the Spring Framework that allows developers to
implement cross-cutting concerns (like logging, security, transaction management,
etc.) separately from the main business logic. It promotes modularity by allowing
the separation of concerns.
---
1. **Aspect**
- A modular unit of cross-cutting concern.
- Example: Logging, security, or transaction management.
- Declared using `@Aspect` annotation in Spring.
2. **Join Point**
- A specific point during the execution of a program, such as the execution of a
method or the handling of an exception.
- Spring AOP supports method execution join points.
3. **Advice**
- Action taken by an aspect at a particular join point.
- Types of Advice:
- **Before Advice**: Executes before the method.
- **After Advice**: Executes after the method.
- **After Returning Advice**: Executes after the method successfully returns.
- **After Throwing Advice**: Executes if the method throws an exception.
- **Around Advice**: Executes before and after the method.
4. **Pointcut**
- A predicate or expression that matches join points.
- Example: Match all methods in a specific package or class.
5. **Weaving**
- The process of linking aspects with other application types or objects to
create an advised object.
- Spring AOP uses **runtime weaving** (proxies).
---
---
@Aspect
@Component
public class LoggingAspect {
// Pointcut: Executes for all methods in the service package
@Before("execution(* com.example.service.*.*(..))")
public void logBeforeMethod() {
System.out.println("Logging before method execution...");
}
}
```
@SpringBootApplication
public class AopExampleApplication {
public static void main(String[] args) {
SpringApplication.run(AopExampleApplication.class, args);
}
}
```
@Service
public class MyService {
public void performTask() {
System.out.println("Executing task...");
}
}
```
#### **Output:**
```
Logging before method execution...
Executing task...
```
---
---
---
---