Spring AOP (1): Overview

1. First impression of AOP

First, give a more professional term (from Baidu):

在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方
式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个
热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑
的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高
了开发的效率。

Then we give a relatively easy to understand example (from: Spring AOP ):

To understand aspect programming, you need to understand what an aspect is. Use a knife to divide a watermelon into two pieces, and the cut is the noodles; for cooking, the pot and the stove work together to complete the cooking, and the pot and the stove are the noodles. In the web-level design, web layer -> gateway layer -> service layer -> data layer, each layer is also an aspect. In programming, between object and object, method and method, module and module are all aspects.

When we generally do activities, we generally check the validity of each interface (whether it starts, whether it ends, etc.), and whether this interface requires a user to log in.

According to normal logic, we can do this. 
Write picture description here

The problem with this is how many code copies are required for how many interfaces there are. For a "lazy person", this is intolerable. Ok, come up with a public method, each interface to call this interface. It smells like sliced ​​noodles. 
Write picture description here

There is also a problem. Although I don't need to copy the code every time, I have to call this method for every interface. So there is the concept of aspect, I inject the method into a certain place of the interface call (pointcut).

Write picture description here

In this way, the interface only needs to care about the specific business, and does not need to care about other logic or processing that is not the interface. 
The red box is aspect-oriented programming.

Two, related concepts in AOP

After seeing the above example, I think everyone has a rough idea of ​​AOP in their minds, but there are some ambiguities in terms such as the aspect mentioned above. Next, let’s explain the related AOP. Concepts. Only by understanding the concepts in AOP can you truly grasp the essence of AOP. 
Here is a more professional concept definition first :

  • Aspect(Aspect): Aspect declaration is similar to class declaration in Java. Aspect will contain some Pointcut and corresponding Advice.
  • Joint point(Connection point): Represents a clearly defined point in the program, typically including method invocation, access to class members and execution of exception handler blocks, etc. It can also nest other joint points.
  • Pointcut(Point of contact): Represents a group of joint points, which are combined through logical relationships, or gathered through wildcards, regular expressions, etc. It defines where the corresponding advice will occur.
  • Advice(Enhancement): Advice defines the Pointcut specific operations to be done at the program points defined in  it. It uses before, after and around to distinguish whether it is before, after, or instead of executing code for each joint point.
  • Target(Target): Advice The target of weaving  .
  • Weaving(Weaving):  the process of Aspect connecting with other objects and creating  Adviced object

Then give an easy-to-understand example

After reading the theoretical part of the knowledge above, I believe that many friends will still feel that the concept of AOP is still very vague, and the understanding of various concepts in AOP is not very thorough. In fact, this is normal, because the concept in AOP is too much and I was the boss also spent hard time combing clear. 
Now I take a simple example to describe what the AOP  AspectJoint pointPointcut and  Advicethe relationship between. 
let's say, there was once a man named Java In the small county town, on a dark and windy night for a month, a murder occurred in this county town. The murderer was very cunning, and there were no valuable clues left. Fortunately, the old Wang who had just returned from next door happened to be here. At that time, I accidentally discovered the murderer's assault process, but because it was late and the murderer was covering his face, Pharaoh didn't see the murderer's face clearly. He only knew that the murderer was a male who was about seven feet five inches tall. A county in Java County. According to Lao Wang’s description, Ling gave an order to the soldiers guarding the gate: All men who were found to be seven feet and five inches tall must be arrested and interrogated. Of course, the soldiers did not dare to violate the order of the county magistrate, so they had to make all those who entered and left the city meet the conditions The people were arrested.

To make us look in the end what is the correspondence between the above a little story and AOP. 
First of all we know, in Spring AOP  Joint point to refer to that point of execution of all methods, and point cut is a descriptive information, it modifies is  Joint point, by Point cut, we can determine which  Joint point can be woven  Advice. Corresponding to the example we gave above, we can make a simple analogy, which  Joint point is equivalent to the people in a small county in Java , which pointcut is equivalent to the accusation made by Lao Wang , That is, the murderer was a male with a height of about seven feet and five inchesand  Advice he was applied to the suspect in accordance with the actions described by Lao Wang: catch it for interrogation
Why can it be compared like this?

  • Joint point : People in a small county in Java: Because by definition, they  Joint point are all Advice candidate points that may be woven  . In Spring AOP, all method execution points can be considered  Joint point. And in our example above, the murder occurred at In a small county, it stands to reason that everyone in this county may be a suspect.

  • Pointcut : Male, about seven feet five inches tall: We know that all methods (joint points) can be woven  Advice, but we don’t want to woven in all methods  Advice, but  Pointcut the function is to provide a set of rules to match joinpoint, Add the joinpoint that meets the rules  Advice. In the same way, for the county magistrate, no matter how faint he is, he also knows that all the people in the county can not be arrested for questioning, but based on the murderer is a male, about seven feet and five inches tall. The person who meets the conditions is arrested. Here the murderer is a male. The height of about seven feet and five inches is a modifier predicate. It limits the scope of the murderer. People who meet this modification rule are all suspects and need to be arrested for interrogation.

  • Advice : Grabbing and interrogating  Advice is an action, that is, a piece of Java code that acts on those defined by the point cut  Joint point . Similarly, in contrast to our example, grabbing and interrogating this action is to affect those Meet the men, the people in the small county town of Java, about seven feet five inches tall.

  • Aspect:: It Aspect is Advice a combination of point cut and  , so here we can make an analogy: "According to the clues of Lao Wang, all men who are found to be 7 feet 5 inches tall must be caught and interrogated." This whole action can be considered as one  Aspect.

Finally is a diagram describing the relationship between these concepts
Write picture description here

Three, some other content

AOPThere Joinpointcan be many types in: constructor call, field setting and acquisition, method call, method execution, exception handling execution, class initialization. That is to say, in AOPthe concept of the above, we can Joinpointweave our custom on the above Advice, but Springit does not implement all of the above joinpoint, to be precise, Springonly supports the method execution type Joinpoint.

Types of Advice

  • before advice, The advice that is executed before the join point. Although before advice is executed before the join point, it cannot prevent the execution of the join point unless an exception occurs (that is, in the before advice code, we cannot artificially determine whether Continue to execute the code in join point)

  • after return advice, Advice executed after a join point returns normally

  • after throwing advice, Advice executed after a join point throws an exception
  • after(final) advice, Regardless of whether a join point exits normally or an exception occurs, the advice will be executed.
  • around advice, The advice that is executed before the join point and after the joint point exits. This is the most commonly used advice.
  • introduction, Introduction can add new properties and methods to the original object.

In Spring, it is realized through dynamic proxy and dynamic bytecode technology AOP. We will explain these contents later.

Guess you like

Origin blog.csdn.net/sanmi8276/article/details/108618572