This is the 7th day of my participation in Gwen Challenge
AOP: Section-oriented programming as opposed to OOP object-oriented programming. Spring’s AOP exists for decoupling. AOP allows a set of classes to share the same behavior. AOP makes up for OOP by only inheriting classes and implementing interfaces, which increases the degree of coupling between codes, and classes can only be inherited singly, preventing more behavior from being added to a set of classes.
First, my first impression of AOP
AOP for the abbreviation of Aspect Oriented Programming, meaning: section-oriented Programming, through pre-compilation and runtime dynamic proxy to achieve unified maintenance of program functions of a technology. AOP is a continuation of OOP, a hot topic in software development, and an important content in Spring framework. It is a derivative paradigm of functional programming. Using AOP, each part of the business logic can be isolated, thus reducing the degree of coupling between each part of the business logic, improving the reusability of the program, and improving the efficiency of development.
Then let’s take an easier to understand example (from: Spring AOP) :
To understand aspect programming, you need to understand what aspect is. Divide a watermelon into two pieces with a knife, and the cut is the cut surface; Cooking, pot and stove together to complete the cooking, pot and stove are the noodles. In web hierarchy design, the Web layer -> Gateway layer -> services layer -> data layer, each layer is also a section. In programming, object to object, method to method, module to module are all facets.
When we do activities, we usually check the validity of each interface (whether it starts, whether it ends, etc.) and whether the interface requires user login.
Logical, we could do that.
The problem with this is that there are as many interfaces as there are code copies. For a “lazy person”, this is intolerable. Well, come up with a common method that each interface calls. There’s a little bit of a cut in there.
The problem is that I don’t have to copy the code every time, but every interface has to call this method. Here comes the concept of facets, where I inject a method somewhere in the interface call (the pointcut).
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 concern of the interface. In the red box, that’s programming for facets.
Second, related concepts in AOP
Having seen the above examples, I think you have a rough prototype of AOP in your mind, but there are some vague places for the terms like the aspects mentioned above. Next, we will explain the relevant concepts in AOP, and understand the concepts in AOP in order to truly grasp the essence of AOP. Here is a professional definition:
- Aspect: An Aspect declaration is similar to a Java class declaration in that it contains pointcuts and Advice.
- Join point: Represents a point explicitly defined in a program, typically including method calls, access to class members, execution of exception handler blocks, etc. It can also nest other Join points.
- Pointcut: REPRESENTS a set of join points, either grouped by logical relationships or grouped by wildmatches, regular expressions, and so on, that define where the corresponding Advice will take place.
- Advice (enhanced) : Advice defines exactly what to do at a point defined in the Pointcut, using before, after, and around to distinguish between executing code before, after, or instead of each join point.
- Target: The Target object to which Advice is woven.
- Weaving: The process of connecting aspects to other objects and creating Adviced Objects
Then take an easy to understand example (from: AOP of Spring) :
After reading the above theoretical part of the knowledge, I believe that there will still be many friends feel that the concept of AOP is still very vague, the various concepts of AOP understanding is not very thorough. This is normal because there are so many concepts in AOP
Let me use a simple analogy to illustrate the relationship between aspects, Join point, Pointcut and Advice in AOP.
Once upon a time, there was a small county called Java. On a dark and windy night, a murder took place in the county. The murderer was so cunning that there were few valuable clues left at the scene. Fortunately, however, Lao Wang, who had just come back from the next door, accidentally discovered the murderer at this time, but because it was late and the murderer was masked, Lao Wang did not see the face of the murderer, only that the murderer was a male, about seven feet and five inches tall. According to Lao Wang’s account, the magistrate of Java county ordered the soldiers guarding the gate to arrest and interrogate any male seven feet and five inches tall. Of course, the soldiers did not dare to disobey the magistrate’s order, so they had to arrest all eligible people entering or leaving the city.
Let’s take a look at the correspondence between one of the above stories and AOP.
In Spring AOP, Join point refers to the execution point of all methods, while Point cut is a description that modifies Join point. We can then determine which Join points can be woven into Advice.
As a simple analogy, Join Point is the people of a small town in Java, and Pointcut is Lao Wang’s accusation that the murderer is a male, about seven feet five inches tall, Advice, on the other hand, is an action that fits Lao Wang’s description of a suspect: bring him in for questioning. Why is that an analogy?
Since a Join point is, by definition, all candidate points that can be woven into Advice, in Spring AOP you can consider all method execution points to be Join points. In our example, the murder took place in a small county, and everyone in that county could be a suspect.
Pointcut: male, 7 ‘5 “: We know that all join points can be woven into Advice, but we do not want to weave Advice into all methods, and the Pointcut provides a set of rules to match
For joinPoint, add Advice to joinPoint that meets the rule. Similarly, for the magistrate, no matter how stupid he was, he knew that he could not arrest all the people in the county and interrogate them. Instead, he would arrest those who met the criteria based on the fact that the murderer was male and about seven feet and five inches tall. In this case, the murderer is male and about seven feet and five inches tall is a modifier, which limits the scope of the killer. Any civilians who meet this modifier rule are suspects and should be arrested and questioned.
Advice is an action, that is, a piece of Java code that works on the Join points defined by the Point cut. Similarly, in contrast to our example, the act of capture and interrogation was applied to the population of a small town in Java, which was mostly male, about seven feet and five inches tall.
According to Lao Wang’s cue, every man found seven feet and five inches tall should be arrested and interrogated.
Finally, a diagram describing the relationship between these concepts:
Three, something else
Joinpoint in AOP can have many types: constructor method calls, field setting and fetching, method calls, method execution, exception handling execution, and class initialization. That is, in AOP concepts we can weave our own Advice into the above joinPoints, but in Spring we do not implement all of the above JoinPoints. To be exact, Spring only supports method execution type JoinPoints.
The type of Advice
-
Before advice, the advice to be executed before join point. Although before advice is executed before join point, it does not prevent the join point from executing unless an exception occurs (i.e., in the before Advice code, You cannot artificially decide whether to continue executing code in join Point.)
-
After return advice, advice performed after a join point returns normally
-
After Throwing advice, the advice that is executed when a join point throws an exception
-
After (final) advice, advice that is executed whether a join point exits normally or if an exception occurs.
-
Around advice, which is executed before and after the join point exits. This is the most commonly used advice.
-
Introduction, introduction can add new properties and methods to existing objects.
AOP is implemented in Spring through dynamic proxy and dynamic bytecode technologies, and different versions of Spring have different execution orders, which we’ll cover later.