Loading
JoinPoints in AOP
In Spring AOP (Aspect-Oriented Programming), a JoinPoint is a specific point in the program flow where an aspect’s advice can be applied.
You can think of it as the exact moment during execution, like when a method is called, where you can insert extra behavior (logging, security, etc.).


In simple words

  • It is the place in your code where cross-cutting logic (aspect) joins the main application logic.
  • For example, right before a method runs or after it finishes.



Example: Logging Before and After a Fund Transfer


Aspect Class with JoinPoints

package com.example.bank.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {

    @Before("execution(* com.example.bank.BankService.transferFunds(..))")
    public void logBeforeTransfer(JoinPoint joinPoint) {
        System.out.println("Before transferring funds...");
    }

    @After("execution(* com.example.bank.BankService.transferFunds(..))")
    public void logAfterTransfer(JoinPoint joinPoint) {
        System.out.println("After transferring funds...");
    }
}


Service Class

package com.example.bank;

import org.springframework.stereotype.Service;

@Service
public class BankService {

    public void transferFunds(double amount, String fromAccount, String toAccount) {
        System.out.println("Transferring $" + amount + " from " + fromAccount + " to " + toAccount);
    }
}

Output

Before transferring funds...
Transferring $100.0 from SavingsAccount to CheckingAccount
After transferring funds...



Key Point

JoinPoint is the moment when the aspect’s code (like logging) actually runs, such as

  • Before a method starts
  • After it ends
  • When it throws an exception

Using join points, Spring AOP helps keep your business logic clean by moving repeated tasks (like logging) to separate classes called aspects.