Aspect Oriented Programming (AOP)

Aspect Oriented Programming (AOP), it is like to classify the system according to the function, each category is a “Aspect”, we make corresponding rules for different aspects, similar development mode is called Aspect Programming.

AOP usage scenarios

  • Logging system
  • Safety and unified effect

AOP advantages

  • Troubleshoot a certain type of problems in a centralized manner, facilitating maintenance
  • Clearer logic
  • Reduce coupling between modules

AOP related concepts

  • Join point: a point during the execution of a program, such as when executing a method or handling an exception. In Spring AOP, a Join point always represents the execution of a method.
  • Advice: notifications can be divided into pre-method notification, post-method notification, and surround notification. Many AOP frameworks, including Spring, model advice as interceptors, maintaining a series of interceptors around join points (forming a chain of interceptors) that enhance join point methods.
  • Pointcut: An expression matching a Join point that is at the heart of AOP, and Spring uses AspectJ as the Pointcut expression language by default.
  • Aspect: An Aspect, a modular concern that spans multiple classes, is an abstraction of Advice and Pointcut that defines a Pointcut to match a Join point, which is the definition of methods that need to be intercepted.
  • Target object: A Target object that is notified by one or more aspects, that is, an object that needs to be intercepted by AOP to enhance a method (using advice). Also known as a notified object. Because runtime proxies are used in AOP, the target object is always the proxied object.
  • AOP Proxy: AN AOP proxy that uses the AOP framework to create an object to implement Aspect functionality. In the Spring framework, an AOP proxy refers to either the JDK’s own dynamic proxy or the CGLIB implementation’s dynamic proxy.
  • Weaving: The process of adding facets to objects and creating proxy objects.
  • Advisor: An Advisor is equivalent to a small aspect, except that it has only one Advice. Advisors are often encountered in transaction management.

AspectJ annotations

  • @before – Pre-notification, called Before join point methods;
  • @around – Around notification, which overwrites the old method, but allows you to call the old method through reflection;
  • @after – post-notification, called After join point methods;
  • @afterRETURNING – returns a notification that is called after the join point method has executed and returned normally, requiring that no exceptions have occurred in the join point method during execution;
  • AfterThrowing – Exception notification, called when join point methods are abnormal.

— the end —

If you have any questions, please leave a comment or send an email. Thank you for reading