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.
- Java JDK 15
- SpringBoot
- Maven
- Use Maven for the build process:
mvn clean install
mvn spring-boot:run
- Find the Swagger-UI API under: http://localhost:8080/swagger-ui.html
- 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
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.
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.