Spring Boot AOP Exception Handling (2024)
In this tutorial, we'll see how to use aspectj @AfterThrowing
annotation to handle
cross-cutting problems like exceptions in the application.
Utilize the previous Spring Boot + AOP + Before After Advice Example to
add the AfterThrowing
Advice.
We can also use aspectj @Around
annotation advice to measure method execution time as a logging in the application.
Refer Spring Boot + AOP + Logging Example
Let's start developing Spring Boot application with AOP.
Project Structure
Maven Dependency
The spring-boot-starter-aop
dependency must be included when introducing AOP in
spring boot. Spring-aop
and aspectjweaver
dependencies are imported
into the application.
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.techgeeknext</groupId>
<artifactId>SpringBoot-AOP-Before-After-Example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringBoot-AOP-Before-After-Example</name>
<description>SpringBoot AOP Logging Example</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
</project>
Take a look at our suggested posts:
Create Aspects
In spring boot, an aspect can be created using the @Aspect
annotation and
registered with a bean
container using the @Component
annotation.
We can create advices as needed within the aspect class. For example, the following class applies to
methods in all classes in the com.techgeeknext
package. For any exception that occurs
in the application for the applied pointcut, the logger/business logic will be applied in
AfterThrowing
method call.
Refer Spring Boot Email Example to send email in case of any error or exception.
package com.techgeeknext.aop;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
@Slf4j
public class AfterThrowingAspectExample {
//could be written as this as well\
//@AfterThrowing("execution(* com.techgeeknext..*(..)))")
@AfterThrowing(pointcut = "execution(* com.techgeeknext..*(..)))", throwing = "ex")
public void logAfterThrowingExceptionCall(Exception ex) throws Throwable {
log.info("======TECHGEEKNEXT - Spring Boot AOP After Throwing Exception Example ");
log.info(ex.getMessage());
//handle exception with business logic like sending notification or default message
}
}
Employee Service for AfterThrowing Advice
We'll create simple service so that we can test Exception with AOP AfterThrowing Advice.
package com.techgeeknext.service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class EmployeeService
{
public Object getEmployeeById(Integer empId) throws Exception {
log.info("=====getEmployeeById Method====");
throw new Exception("Exception occurred while retrieving employee.");
}
}
Create test class to test the AOP AfterThrowing Advice.
package com.techgeeknext;
import com.techgeeknext.service.EmployeeService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AopAfterThrowingTest
{
@Autowired
private EmployeeService employeeService;
@Test(expected = Exception.class)
public void testGetEmployeeById() throws Exception {
employeeService.getEmployeeById(123);
}
}
Test the Spring AOP Exception handling through AfterThrowing Advice
To launch the spring boot application, run the mvn spring-boot run
command. When we run the
AopAfterThrowingTest
class, the console output shows the AOP AfterThrowing Advice logger statement
with an exception raised from the service class.
Download Source Code
The full source code for this article can be found on below.Download it here - Spring Boot AOP Exception Handling Example