Handling Exception Using Spring's Aspect Oriented Programming

In this post, I will show you how we can handle exceptions using springs aspect-oriented programming.
Prerequisites
To follow this article you should have

  • JDK 8+  
  • gradle 2.3 + 
  • Your favorite IDE (I used IntelliJ Idea)
Folder Structure
The folder structure should be as follows
 aopexeptionhnadling
├── build.gradle
└── src
    ├── main
    │   └── java
    │       └── net
    │           └── asifhossain
    │               └── aopexceptionhandling
    │                   ├── aop
    │                   │   └── ExceptionLoggerPointCut.java
    │                   ├── AopExampleApp.java
    │                   └── service
    │                       └── ExceptionalService.java
    └── test
        └── java
            └── net
                └── asifhossain
                    └── aopexceptionhandling
                        └── AopExampleAppTest.java
Creating Gradle Build File
Create a file named build.gradle in application with the following content
build.gradle


Creating the service
Now let's create a service method which will throw exception


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


Create The Application class
Now create the application class with all the component
AopExampleApp.java


Run The Application
To run the application run the following command
gradle bootRun
You will see the thrown application is logged on the console.
That's all for today. Thanks for reading.
You can find all the codes in my GitHub repository
https://github.com/mirmdasif/springmvc/tree/master/aopexceptionhandling

Comments

Popular posts from this blog

Refactoring Comparators in Java 8

What is a View in oracle?