Aspect Oriented Programming to trace the requests and responses to a REST method in Spring Boot.

 

Aspect Oriented Programming (AOP) is one of the key feature of Spring framework. AOP is similar to OOPS concept where it breaks code into multiple reusable modules.

AOP provides the capability to dynamically add module at runtime.

This is like injecting complete module(logging, cahcing, etc ) in Spring framework dynamically.

Logging, caching, security, monitoring, etc. are some of the examples cross cutting concern from AOP. At any point of time there modules can be added dynamically.

Spring AOP has interceptors which can intercept application and its methods. This is to perform some extra action at the time of property initiation, method initialization or destroy.

Let's try it to create a custom annotation to trace the requests and responses to a REST method in Spring Boot

At first you we need to use spring-boot-starter-web and spring-boot-starter-aop dependencies to make it work.


STEP1: An interface with the annotation name @Traceable

Now , in whichever method you add the above annotation , the method suddenly transforms into a traceable one. The requests and responses are traced automatically!

 

STEP2: An Aspect to trace the requests and responses

To make use of the traceable annotation , we need to build the logic inside an Aspect class

 

STEP3: Add the annotation


That’s it!


Comments

Popular posts from this blog

Debug Java Stream in Intellij Idea

Creating Efficient Docker Images with Spring Boot 2.3

CRUD Goes Even Easier With JPABuddy