Giter Site home page Giter Site logo

3umarg / aop-springboot Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 521 KB

GitHub Repository for summarizing the basic concepts of Aspect Oriented Programming ( AOP ) and showing how Spring Framework implements it, all examples are from -Spring Start Here- Book.

Java 100.00%

aop-springboot's Introduction

AOP in Spring Boot

What is AOP ??

  • Aspects are a way the framework intercepts method calls and possibly alters the execution of methods. You can affect the execution of specific method calls you select. This technique helps you extract part of the logic belonging to the executing method. In certain scenarios, decoupling a part of the code helps make that method easier to understand.
  • It allows the developer to focus only on the relevant details discussed when reading the method logic.
  • Sometimes it’s not relevant to have parts of the code in the same place with the business logic because it makes the app more difficult to understand "Single Responsibility". A solution is to move part of the code aside from the business logic implementation using aspects.
  • Also, to apply Decoupling.

How aspects work in Spring ??

  1. aspect : the logic you need to perform and separate aside from the main method (e.g. logging / authentication / proxies ... etc.)
  2. advice : when the timing of implement the aspect from the target/main method (e.g. before/after/during ... etc.)
  3. pointcut : which method you want to intercept with your aspect
  4. join point : which state you want to intercept the pointcut (e.g. execution,afterReturned,afterThrown)
  5. target object : the bean contains the pointcut/actual method.

The aspect terminology. Spring executes some logic (the aspect) when someone calls a specific method (the pointcut). We need to specify when the logic is executed according to the pointcut (e.g., before). The when is the advice. For Spring to intercept the method, the object that defines the intercepted method needs to be a bean in the Spring context. So, the bean becomes the target object of the aspect.

aspects.png


How Spring deals with Aspect targets ??

Weaving an aspect. Instead of giving you a reference to the real bean/target , Spring gives you a reference to a proxy object, intercepts the method calls, and manages the aspect logic.

aspects 2.png


When a method isn’t aspected, the call goes directly to that method. When we define an aspect for a method, the call goes through the proxy object. The proxy object applies the logic defined by the aspect and then delegates the call to the real method.

aspects 3.png



Implementing aspects with Spring AOP

To implement an aspect, you follow four easy steps:

  1. First, you need to enable the aspect capability in your app.
  2. Then you create an aspect class,
  3. define a method, and instruct Spring when and what to intercept.
  4. Finally, you implement the aspect logic.

aspects 4.png


References

  • Spring Start Here Book : 6th chapter.

aop-springboot's People

Contributors

3umarg 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.