AOP is Aspect Oriented Program for Aspect programming
Section-oriented programming
As we all know, Java is an object-oriented (OOP) programming language. Object orientation is characterized by encapsulation, inheritance and polymorphism. Encapsulation is to encapsulate different functions into different objects (responsibility assignment, such as login and deletion, will be encapsulated into independent classes), which increases code reuse and reduces code complexity. But in the code of responsibility assignment, part of the code is also increased repeatability. For example, when adding code for performance statistics, logging, and transaction management, you need to add duplicate code to the code file for each function. Of course, you could repackage this part of the code into a new class, but that would result in coupling of the separate functional-encapsulation classes above. So how do you throw code into your code without breaking the encapsulation of your class? This idea of dynamically cutting code into a class’s specified methods and locations at run time is faceted programming. Section-oriented programming is based on dynamic proxy implementation. OOP separates classes vertically, while AOP adds specific code horizontally. OOP:
AOP terms:
-
Joinpoint: Interception requires a strong method. Where can I go to be zeng Qiang
-
Pointcut: Pointcuts, which methods in which classes in which packages, can be considered a collection of join points.
-
Advice: enhance when and what the method executes after interception to Joinpoint. According to the timing, it can be divided into: pre-enhancement, post-enhancement, abnormal enhancement, final enhancement and surround enhancement.
-
Aspect: Pointcut+Advice, where to + when + what enhancement to do
-
Target: indicates the Target object that is proxied
-
Weaving: the process of creating a Proxy object after adding Advice to Target
-
Proxy: A Proxy class generated when a class is woven into AOP for enhancement.
AOP enhancement details
To enhance | note | Application scenarios |
---|---|---|
Aop: Before (pre-enhanced) | Perform the enhancement before the method executes | Permission control, call logging, etc |
Aop: After-returning (Post-enhanced) | Perform the enhancement after the method completes normal execution (no exceptions encountered) | Statistical analysis of the result data |
Aop: Throwing (Exception Enhancement) | An enhancement is performed when a method throws an exception to exit | Log the exception information |
Aop: After (Final enhancement) | After the method executes, it is equivalent to executing inside finally; You can configure Throwing to obtain resources for intercepting exceptions | |
Aop: Around (Surround Enhancement) | One of the most powerful types of reinforcement, wrap reinforcement, allows for custom behavior before and after a method call. Wrap reinforcement has two requirements: 1. 2. The first argument to the method must be ProceedingJoinPoint. | Caching, performance logging, permissions, transaction management, etc |