Advanced Aspect-Oriented Programming (AOP) Features with Spring Boot | part 2

mohammed alaa
3 min readJan 24, 2024

--

in part 1of this tutorial we explored the basics of Aspect-Oriented Programming , In this tutorial, we’ll explore some advanced features of Aspect-Oriented Programming (AOP) using Spring Boot. We’ll cover pointcuts, advice types, and other advanced aspects to enhance your understanding of AOP.

Step 1: Define a More Complex Service

Let’s start by creating a more complex service with multiple methods. Update your MathService class:

@Service
public class MathService {
public int add(int a, int b) {
return a + b;
}

public int subtract(int a, int b) {
return a - b;
}

public int multiply(int a, int b) {
return a * b;
}

public int divide(int a, int b) {
if (b == 0) {
throw new IllegalArgumentException("Cannot divide by zero");
}
return a / b;
}
}

Step 2: Using Pointcuts

Pointcuts allow you to specify where in your code an advice should be applied. Let’s create an aspect that logs method executions only for the add and subtract methods.

@Aspect
@Component
public class LoggingAspect {

@Before("execution(* com.yourpackage.MathService.add(..)) || execution(* com.yourpackage.MathService.subtract(..))")
public void logAddAndSubtract(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
System.out.println("Logging for " + methodName);
}
}

Replace com.yourpackage with the actual package name of your MathService.

Step 3: Using Different Advice Types

AspectJ supports different types of advice such as @Before, @After, @AfterReturning, @AfterThrowing, and @Around.

Let’s modify our aspect to use different advice types:

@Aspect
@Component
public class LoggingAspect {

@Before("execution(* com.yourpackage.MathService.add(..))")
public void logBeforeAdd(JoinPoint joinPoint) {
System.out.println("Before add method");
}

@AfterReturning(pointcut = "execution(* com.yourpackage.MathService.subtract(..))", returning = "result")
public void logAfterReturningSubtract(JoinPoint joinPoint, Object result) {
System.out.println("After returning from subtract method. Result: " + result);
}

@AfterThrowing(pointcut = "execution(* com.yourpackage.MathService.multiply(..))", throwing = "exception")
public void logAfterThrowingMultiply(JoinPoint joinPoint, Exception exception) {
System.out.println("After throwing exception in multiply method: " + exception.getMessage());
}

@After("execution(* com.yourpackage.MathService.divide(..))")
public void logAfterDivide(JoinPoint joinPoint) {
System.out.println("After divide method");
}
}

Step 4: Run and Test

Run your Spring Boot application and test the various methods in the MathService. Observe the console logs to see how different advice types are applied based on the method execution.

Step 5: Combining Multiple Aspects

You can create multiple aspects and apply them selectively to different parts of your code. For example, let’s create a new aspect for performance monitoring:

@Aspect
@Component
public class PerformanceAspect {

@Around("execution(* com.yourpackage.MathService.*(..))")
public Object measurePerformance(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object result = proceedingJoinPoint.proceed();
long endTime = System.currentTimeMillis();
System.out.println("Method execution time: " + (endTime - startTime) + " milliseconds");
return result;
}
}

Step 6: Run and Observe

Run your Spring Boot application again, and now you’ll see both the LoggingAspect and PerformanceAspect being applied based on the specified pointcuts.

Congratulations! You’ve now explored advanced features of Aspect-Oriented Programming with Spring Boot, including custom pointcuts, different advice types, and combining multiple aspects. As you continue to work with AOP, consider applying these concepts to real-world scenarios, such as logging, performance monitoring, and error handling, to enhance the molecularity and maintainability of your applications ,
happy coding and see you soon .

--

--

mohammed alaa

"Passionate software developer skilled in Java, Spring boot and more! 💻 Bringing innovation to life, one line of code at a time. #SoftwareDeveloper