Practical Spring AOP Implementation: A Clear Guide

AOP enables method enhancement without modifying the original source code (enhancement = proxy).

AOP Pointcut Expression Syntax:

execution([access modifier] [return type] [full class path] [method name] ([parameter list]))

Example:

@Before(value = "execution(* com.example.service.AccountService.deposit(..))")

Join Point Any method within a class that can potentially be enhanced is called a join point.

Pointcut The specific methods that are actually enhanced are referred to as pointcuts.

Advice The actual enhancement logic is called advice.
There are several types of advice: Before, After Returning, Around, After Throwing, and After.

#Before @Before: Executes before the target method #After Returning @AfterReturning: Executes after the target method completes successfully #Around @Around: Executes both before and after the target method #After Throwing @AfterThrowing: Executes when the target method throws an exception #After @After: Executes regardless of whether an exception occurs, similar to finally block

Aspect The process of applying advice to pointcuts is known as an aspect.

Below is a practicla example:

Target Class (to be enhanced)

@Component
public class AccountService {
    public void deposit() {
        System.out.println("Processing deposit transaction.......");
    }
}

Aspect Class (contains the advice)

@Component
@Aspect
public class TransactionLogger {
    
    @Before(value = "execution(* com.example.service.AccountService.deposit(..))")
    public void logBefore() {
        System.out.println("Logging transaction start.....");
    }
    
    @After(value = "execution(* com.example.service.AccountService.deposit(..))")
    public void logAfter(){
        System.out.println("Logging transaction end......");
    }
    
    @AfterReturning(value = "execution(* com.example.service.AccountService.deposit(..))")
    public void logAfterReturning(){
        System.out.println("Transaction completed successfully.........");
    }
    
    @AfterThrowing(value = "execution(* com.example.service.AccountService.deposit(..))")
    public void logAfterThrowing(){
        System.out.println("Transaction failed with exception.........");
    }
    
    @Around(value = "execution(* com.example.service.AccountService.deposit(..))")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("Starting transaction monitoring.........");

        Object result = joinPoint.proceed();

        System.out.println("Transaction monitoring completed.........");
        return result;
    }
}

Testing with Spring Container

public class AopDemoTest {
    @Test
    public void demonstrateAop(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService service = (AccountService)ctx.getBean("accountService");
        service.deposit();
    }
}

Execution Output

Starting transaction monitoring.........
Logging transaction start.....
Processing deposit transaction.......
Transaction completed successfully.........
Logging transaction end......
Transaction monitoring completed.........

Tags: Spring aop aspectj java Enterprise

Posted on Sun, 10 May 2026 07:57:51 +0000 by aahh