/aop-examples

Some examples of Aspect Oriented Programming (AOP) with SpringBoot

Primary LanguageJava

Examples of Aspect Oriented Programming (AOP) with SpringBoot

Most people know object oriented programming and functional programming. But what about aspect oriented programming (AOP)? This repository contains 4 fully working examples of AOP, ranging from standard Spring Aspects to custom Aspects.

Dependencies

  • Java JDK 15
  • SpringBoot
  • Maven

Compile and run the web service

  • Use Maven for the build process:
mvn clean install
mvn spring-boot:run

Example REST calls

  • Compute a Fibonacci number:
GET http://localhost:8080/api/fibonacci/40
  • Store a String (simulate concurrency issues)
POST http://localhost:8080/api/storeData?data=hello-world

Aspects for the Fibonacci computation

If you compute the 40th Fibonacci number twice, you will see the following log:

Method [fibonacci] gets called with parameters [40]
Execution took [3379ms]
Method [fibonacci] gets called with parameters [40]
Execution took [3ms]

The following aspects are in play:

  • @Cacheable: This standard-Spring annotation allows to cache previous results. As you can see above, the first REST call is pretty slow, but the second call is super fast.
  • @MonitorTime: We use a custom aspect which measure the total execution time of our REST call and prints the result to the console.
  • @LogMethodName: We use a second custom aspect which prints the name and arguments of our REST controller method.

Aspect for a retry logic

We simulate a dadabase with concurrency issues. Storing a String in our database creates the following log:

Method [storeData] gets called with parameters [hello-world]
Method [storeData] gets called with parameters [hello-world]
Method [storeData] gets called with parameters [hello-world]
Pretend everything went fine

The following aspects are in play:

  • @RetryOperation: This Aspect makes sure that the (idempotent) method is retried until it succeeds. In the log above, you can see that it took 3 tries to store the data.