Git Product home page Git Product logo

aop-examples's Introduction

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.

aop-examples's People

Contributors

pmgysel avatar

Stargazers

miladrostami avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.