Recently our team needed to measure the performance of a client’s application, which heavily depends on third API providers, such as Spotify, Yelp and TripAdvisor. Our goal was to mock the response from these providers in an easy way, which will later help us to measure the performance of the code base without concerning the response time from the third party APIs.My immediate enlightenment was to get advantage of the Aspect Oriented Programming with the spring aop project. The idea is to intercept the calls to the external providers and execute a custom method which will fake their response.
As usual this tutorial can be completed step by step or you can directly clone a simple demo project from https://github.com/boykodimitroff/aspectj-demo
The provided git project is a basic Spring Boot application with included Spring AOP dependency.
Let’s take a look of the following class
eu.dreamix.rest.RestController.java
package eu.dreamix.rest; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; /** * Created by bdimitrov on 6/6/17. */ @RestController @RequestMapping("/rest/") public class SpotifyController { @RequestMapping(value = "/spotify", method = RequestMethod.GET) public ResponseEntity
As I mentioned at the beginning we are going to mock an existing functionality, so SpotifyController.java is just a basic rest controller with one method, the returned object of which will be mocked.
The meaningful work unit regarding our task is in the following class
eu.dreamix.aspect.SpotifyAspect.java
package eu.dreamix.aspect; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.context.annotation.Configuration; import org.springframework.http.ResponseEntity; /** * Created by bdimitrov on 6/6/17. */ @Aspect @Configuration public class SpotifyAspect { @Pointcut("execution(* eu.dreamix.rest.SpotifyController.callSpotify(..))") public void inSpotifyController() {} @Around("inSpotifyController()") public ResponseEntity mockSpotify() { return ResponseEntity.ok("Mocked Spotify call"); } }
Three terms needs clarification here. Aspect, advice and pointcut.
Aspect
The key unit of modularity in Object Oriented Programming is the class, whereas in Aspect Oriented Programming the unit of modularity is the aspect. For example, a logging module would be called AOP aspect for logging. An application can have any number of aspects depending on the requirement.
Advice
This is the actual action to be taken either before or after the method execution. This is an actual piece of code that is invoked during the program execution. There is third type of advice which we are going to use in our demo. It is the Around advice.
Pointcut
Pointcut defines at what point in the application, the associated advice should be applied. You can specify pointcuts using expressions or patterns.
Taking the theory in mind, let’s get back to our aspect.
The expression “execution(* eu.dreamix.rest.SpotifyController.callSpotify(..))” in the @Pointcut declaration tells the framework to catch the execution of a method with name callSpotify in a specified package regarding the return type(*) and method parameters(…). We can define various expressions according to our needs. For example * *.callSpotify(…) will catch all methods in all classes with name callSpotify.
The method marked with @Around annotation is our advice. This means that the real execution of the callSpotify method will be skipped and mockSpotify() method will be called instead. Other advices are @Before and @After which respectively will be called before or after the pointcut defined method and will not skip the method execution.
If you want to get deeper into  Spring AOP you can check this very useful documentation.
Do you find the article useful? Have you used Spring AOP before and how?