Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
After Throwing
advice in Aspect-Oriented Programming (AOP) is executed when a method throws an exception. This kind of advice is useful when you want to execute certain behavior (like logging or notifying) specifically when exceptions occur in your application.
pom.xml
:<!-- Spring Boot Starter AOP --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
Create an aspect that contains the After Throwing
advice:
package com.example.demo.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Aspect @Component public class ErrorLoggingAspect { @AfterThrowing(pointcut = "execution(* com.example.demo.service.*.*(..))", throwing = "ex") public void afterThrowingAdvice(JoinPoint joinPoint, Throwable ex) { System.out.println("An exception has been thrown in " + joinPoint.getSignature().getName() + " with message: " + ex.getMessage()); } }
In this code:
@Aspect
: Marks the class as an aspect.
@Component
: Registers the aspect as a Spring bean.
@AfterThrowing
: Denotes an after-throwing advice. The pointcut
attribute specifies when the advice should run. Here, it'll run when any method within the com.example.demo.service
package throws an exception. The throwing
attribute binds the thrown exception to the ex
parameter of the advice method.
The aspect will be applied wherever the conditions in the pointcut expressions are met. In this case, it'll be applied to methods in the com.example.demo.service
package that throw exceptions.
Now, when you run your Spring Boot application and a method matching the pointcut expression throws an exception, you'll see the log message indicating which method threw the exception and the exception's message.
This After Throwing
advice allows you to centralize your exception logging, making it easier to keep track of and manage exceptions across various components of your application.
Spring Boot AOP After Throwing Advice example:
@Aspect @Component public class AfterThrowingAdviceExample { @AfterThrowing( pointcut = "execution(* com.example.service.MyService.myMethod(..))", throwing = "exception") public void afterThrowingAdvice(Exception exception) { // Code to be executed after an exception is thrown System.out.println("AfterThrowing advice executed. Exception: " + exception.getMessage()); } }
Handling exceptions with After Throwing advice in Spring Boot AOP:
@Aspect @Component public class ExceptionHandlingAfterThrowingAdvice { @AfterThrowing( pointcut = "execution(* com.example.service.MyService.myMethod(..))", throwing = "exception") public void handleException(Exception exception) { // Code to handle the thrown exception System.out.println("Exception handled: " + exception.getMessage()); } }
AspectJ After Throwing advice in Spring Boot:
@Aspect @Component public class AspectJAfterThrowingAdvice { @AfterThrowing( pointcut = "execution(* com.example.service.MyService.myMethod(..))", throwing = "exception") public void afterThrowingAdvice(Exception exception) { // Code to be executed after an exception is thrown System.out.println("AfterThrowing advice executed. Exception: " + exception.getMessage()); } }
Logging and error handling with After Throwing advice in Spring Boot AOP:
@Aspect @Component public class LoggingAndErrorHandlingAfterThrowingAdvice { @AfterThrowing( pointcut = "execution(* com.example.service.MyService.myMethod(..))", throwing = "exception") public void logAndHandleError(Exception exception) { // Log the exception and handle the error System.out.println("Error occurred: " + exception.getMessage()); } }
Using @AfterThrowing advice with Spring Boot AOP:
@AfterThrowing
is a specialized form of @After
advice.@Aspect @Component public class AfterThrowingAdviceExample { @AfterThrowing( pointcut = "execution(* com.example.service.MyService.myMethod(..))", throwing = "exception") public void afterThrowingAdvice(Exception exception) { // Code to be executed after an exception is thrown System.out.println("AfterThrowing advice executed. Exception: " + exception.getMessage()); } }
Ordering of multiple After Throwing Advice in Spring Boot AOP:
@Order
annotation to specify the order of execution.@Aspect @Component @Order(2) public class AfterThrowingAdviceExample1 { // Code } @Aspect @Component @Order(1) public class AfterThrowingAdviceExample2 { // Code }
Exception translation with After Throwing advice in Spring Boot:
@Aspect @Component public class ExceptionTranslationAfterThrowingAdvice { @AfterThrowing( pointcut = "execution(* com.example.service.MyService.myMethod(..))", throwing = "exception") public void translateException(Exception exception) throws CustomException { // Translate the exception to a custom exception throw new CustomException("Translated Exception", exception); } }