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 @Component public class LoggingAspect { @Before("execution(*
com.example.service.*.*(..))") public void logBeforeMethodExecution() {
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
public class mydemo{
@Before("execution(public void assAccount())")
public void beforeAddClass(){
....;
}
}
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.
java code:
@Aspect
@Component
public class MyloggingAspect {
@Pointcut("execution(* org.example.ap.Dao.*.*(..))")
public void forDaoPackage() {}
//@Before("execution(public void
org.example.ap.Dao.AccountDao.addAccount())")
//@Before("execution(public void updateAccount())")
//@Before("execution(public void add*())") //@Before("execution(void add*
())") //@Before("execution(* add*(int,..))") @Before("forDaoPackage()")
public void beforeAddAccountAdvice() {
System.out.println("n=====>>> Executing @Before advice on
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.
after throwing an exception.
in all after cases
before and after.

AOP (Aspect-Oriented Programming) spring boot

  • 1.
    AOP (Aspect-Oriented Programming) AOPis 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).
  • 2.
    🔹 Example inJava 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 @Component public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void logBeforeMethodExecution() { System.out.println("Method executed: Logging before execution."); } }
  • 3.
    code for aspectis 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.
  • 4.
    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 public class mydemo{ @Before("execution(public void assAccount())") public void beforeAddClass(){ ....; } }
  • 5.
    A predicate expressionfor where advice should be applied. The pattern is optional if it has ?.
  • 6.
    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.
  • 8.
    java code: @Aspect @Component public classMyloggingAspect { @Pointcut("execution(* org.example.ap.Dao.*.*(..))") public void forDaoPackage() {} //@Before("execution(public void org.example.ap.Dao.AccountDao.addAccount())") //@Before("execution(public void updateAccount())") //@Before("execution(public void add*())") //@Before("execution(void add* ())") //@Before("execution(* add*(int,..))") @Before("forDaoPackage()") public void beforeAddAccountAdvice() { System.out.println("n=====>>> Executing @Before advice on
  • 9.
    Combining pointcuts: How tocontrol 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()"); } }
  • 10.
    Reading method argumentwith joinPoints:
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.