AOP (Aspect-Oriented Programming)
AOP is a programming paradigm that helps separate cross-cutting concerns (like logging,
security, transaction management, etc.) from the core business logic. This improves modularity
and reduces code duplication.
?????? Key Concepts of AOP
1. Aspect – A modular component that implements a cross-cutting concern.
2. Pointcut – Defines where an aspect should be applied (e.g., before or after a method).
3. Advice – The actual code that runs at a pointcut (before, after, around).
?????? Example in Java with Spring AOP
``
Here, the LoggingAspect intercepts the execution of all methods in com.example.service
and logs a message before they are executed.
?????? Advantages of AOP:
✅ Reduces code duplication
✅ Better separation of concerns
✅ Easier maintenance
AOP
Programming technique based on concept of an aspect.
Aspect encapsulates cross-cutting logic.
"concerns" means logic / functionality.
Aspect can be reused at multiple location.
Same aspect/class .. applied based on configuration.
Apply the proxy design pattern
4. Joinpoint – A specific point where an aspect can be applied (e.g., method calls, field
access).
5. Weaving – The process of injecting aspects into the main code (at compile-time, load-time,
or runtime).
@Aspect@ComponentpublicclassLoggingAspect{ @Before("execution(*
com.example.service.*.*(..))") publicvoidlogBeforeMethodExecution(){
System.out.println("Method executed: Logging before execution."); }}
code for aspect is defined in a single class:
1) much better than being scattered everywhere.
2) Promotes code reuse and easier to change.
Businees code in your applicaion is cleaner:
1) Only applies to business functionnality: addAccount.
2) Reduces code complexity.
ADDITIONAL AOP USE CASES:
Advices Types:
Before advice: run before the method.
After finally advice: run after method (finally).
after returning advice : run after method (success execution).
Most common : logging, security, transactions
Audit logging : who, where, when, what.
Exception handling : log exception and notify Devops team via sms/mail.
API management : how many times has a method been called user.
Advantages:
- reusable modules, resolve code tangling, resolve code scatter, applied selectively based
on configuration.
AOP TERMINOLOGY
- aspect : module of code for a cross-cutting concern(logging, security, transaction...).
- Advice : what action is taken and when it should be applied.
- join point : when to apply code during program execution.
- pointcut : A predicate expression for where advice should be applied.
after throwing advice : run after method (if exception thrown).
around advice : run before and after method.
Weaving:
connecting aspects to target objects to create an advised object.
different types of weaving:
compile-time, load-time, run-time.
AOP Frameworks
Spring AOP, AspectJ.
Spring AOP : method-level join points, Run-time code weaving (slower that aspectJ).
Start with Advices_:
Before Advice : run before a method.(@Before)
AOP POINTCUT
@Ascpect
publicclass mydemo{
@Before("execution(public void assAccount())")
publicvoidbeforeAddClass(){
....;
}
}
A predicate expression for where advice should be applied.
The pattern is optional if it has ?.
Parameter pattern wildcards:
(): matches a method with no arguments.
() : matches a method with one argument of any type.
(..) : matches a method with 0 or more argumenets of any type.
Combining pointcuts:
How to control order of advices being applied:
solution is:
1) Refactor: Place advice in separate aspect.
2) Control order on aspects using the @Order annotation
3) Guarantees order of when Aspects are applied.
addAccount()");
}
}
Reading method argument with joinPoints:
More Advices:
@AfterReturning Advice
After successful execution.