Handling Exceptions Using Spring's AOP

DZone 's Guide to

Handling Exceptions Using Spring's AOP

This quick lesson in AOP will explore you can use Spring's implementation of aspect-oriented programming to handle exceptions.

· Java Zone ·
Free Resource

In this post, I will show you how we can handle exceptions using Spring's aspect-oriented programming.


To follow this article, you should have:

  • JDK 8+  
  • Gradle 2.3 + 
  • Your favorite IDE (I use IntelliJ IDEA)

Folder Structure

The folder structure should be as follows.


├── build.gradle
└── src    
        ├── main    
        │   └── java
        │       └── net    
        │           └── asifhossain    
        │               └── aopexceptionhandling    
        │                   ├── aop    
        │                   │   └── ExceptionLoggerPointCut.java    
        │                   ├── AopExampleApp.java    
        │                   └── service    
        │                       └── ExceptionalService.java    
        └── test        
                └── java            
                         └── net                
                             └── asifhossain
                                 └── aopexceptionhandling
                                    └── AopExampleAppTest.java

Creating a Gradle Build File

Create a file named build.gradle in the application with the following content:


group 'net.asifhossain'

buildscript {
    ext {
        springBootVersion = '1.5.9.RELEASE'
    repositories {
    dependencies {

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'net.asifhossain'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {

dependencies {

Creating the Service

Now let's create a service method that will throw an exception.


package net.asifhossain.aopexceptionhandling.service;

import org.springframework.stereotype.Service;

public class ExceptionalService {
    public void throwException() throws Exception {
        throw new Exception();

Now let's create an aspect configuration file to log all the exceptions thrown from the service method.


package net.asifhossain.aopexceptionhandling.aop;

import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Configuration;

public class ExceptionLoggerPointCut {
    @AfterThrowing(pointcut = "execution(* net.asifhossain.aopexceptionhandling.*.*.*(..))", throwing = "ex")
    public void logError(Exception ex) {

Making the Application Runnable

Now create the application class with all the components.


package net.asifhossain.aopexceptionhandling;

import net.asifhossain.aopexceptionhandling.service.ExceptionalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

public class AopExampleApp implements CommandLineRunner {

    ExceptionalService service;

    public void run(String... args) throws Exception {
        try {
        } catch (Exception ex) {

    public static void main(String[] args) {

Running the Application

To run the application, run the following command:

gradle bootRun 

You will see the thrown exception is logged on the console.

You can find all the codes in my GitHub repository.

That's all for today. Thanks for reading.

aop, exception handling, java, spring, tutorial

Published at DZone with permission of Mir Md Asif Hossain . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}