Home > Java > javaTutorial > Detailed explanation of Spring - AOP detailed explanation (AOP overview)

Detailed explanation of Spring - AOP detailed explanation (AOP overview)

(*-*)浩
Release: 2019-09-11 16:33:11
forward
1997 people have browsed it

1. First impression of AOP

Detailed explanation of Spring - AOP detailed explanation (AOP overview)

First give a relatively professional terminology (from Baidu) :

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

Last we give an example that is easier to understand:

To understand aspect programming, you need to first understand what aspects are. Use a knife to divide a watermelon into two halves, and the cut is the cut surface; when cooking, the pot and stove work together to complete the cooking, and the pot and stove are the cut surfaces. In the web layer design, the web layer -> gateway layer -> service layer -> data layer, each layer is also an aspect. In programming, there are aspects between objects, methods and methods, and modules.

When we usually do activities, we usually check the validity of the activity for each interface (whether it starts, whether it ends, etc.), and whether this interface requires user login.

According to normal logic, we can do this.

Detailed explanation of Spring - AOP detailed explanation (AOP overview)

The problem is that as many interfaces as there are, the number of code copies is required. For a "lazy person", this is intolerable. OK, propose a public method, and every interface will call this interface. There's a bit of a sliced ​​noodle flavor here.

Detailed explanation of Spring - AOP detailed explanation (AOP overview) I also have a problem. Although I don’t need to copy the code every time, every interface must call this method. So there is the concept of aspect, and I inject the method into the interface call somewhere (point cut).

Detailed explanation of Spring - AOP detailed explanation (AOP overview)In this way, the interface only needs to care about the specific business, and does not need to pay attention to other logic or processing that is not the concern of the interface.

The red box is aspect-oriented programming.

2. Related concepts in AOP

After reading the above examples, I think everyone already has a rough prototype of AOP in their minds, but they are also There are some ambiguities in terms such as aspects mentioned above. Next, let’s explain the related concepts in AOP. Only by understanding the concepts in AOP can we truly grasp the essence of AOP.

Here is a more professional definition of the concept:

Aspect: Aspect declaration is similar to class declaration in Java, and will be included in Aspect With some Pointcuts and corresponding Advice.

Joint point: Represents a clearly defined point in the program, typically including method invocation, access to class members, and execution of exception handling blocks, etc. It can also nest other joint point.

Pointcut: Represents a set of joint points. These joint points are either combined through logical relationships or concentrated through wildcards, regular expressions, etc. It defines the corresponding Advice to be Where it happens.

Advice (enhancement): Advice defines the specific operations to be performed by the program points defined in Pointcut. It uses before, after and around to distinguish whether the code is executed before, after or instead of each joint point. .

Target (target object): The target object woven into Advice.

Weaving: The process of connecting Aspect to other objects and creating an Advised object

Then give an easy-to-understand example:

After reading the above theoretical knowledge, I believe that many friends still feel that the concept of AOP is still vague, and they do not fully understand the various concepts in AOP. In fact, this is normal, because the concepts in AOP are not very clear. There are so many concepts that it took me a lot of effort to sort them out.

Let me use a simple example to compare the relationship between Aspect, Joint point, Pointcut and Advice in AOP. .

Let us assume that once upon a time there was a small county called Java. On a dark and stormy night, a murder occurred in this county. The murderer was very cunning, and there was nothing traceable at the scene. Fortunately, Lao Wang, who had just returned from next door, accidentally discovered the murderer's murder process at this time. However, because it was late and the murderer was masked, Lao Wang could not see clearly the murderer's face. Based on Lao Wang’s description, the magistrate of Java County issued an order to the soldiers guarding the gate: Any man found to be seven feet five inches tall must be arrested and interrogated. Of course, the soldiers did not dare to disobey the county magistrate's order, so they had to arrest all qualified people entering and leaving the city.

Let us take a look at the above short story and what AOP is about Correspondence.

First of all, we know that in Spring AOP, Joint point refers to the execution point of all methods, and point cut is a description information, which modifies the Joint point. Through point cut, we can determine which Joint points can is woven into Advice. Corresponding to the example we gave above, we can make a simple analogy. Joint point is equivalent to the people in a small county in Java, and pointcut is equivalent to the accusation made by Lao Wang, that is, the murderer is a male. , about seven feet five inches tall, and Advice was applied to the actions of the suspect that fit Lao Wang’s description: arrest him for interrogation.

Why is this analogy possible?

Joint point: People in a small county town in Java: Because according to the definition, Joint point is all candidate points that may be woven into Advice. In Spring AOP, all method execution points can be considered to be Joint points. And In our example above, the murder occurred in a small county. It stands to reason that everyone in this county may be a suspect.

Pointcut: Male, about 7 feet 5 inches tall: We know, Advice can be woven into all methods (joint points), but we do not want to weave advice into all methods. The role of Pointcut is to provide a set of rules to match joinpoints, and add advice to joinpoints that meet the rules. Similarly. , for the county magistrate, no matter how stupid he is, he knows that he cannot arrest all the people in the county for interrogation. Instead, he should arrest those who meet the conditions based on the fact that the murderer is a male and is about seven feet five inches tall. Here the murderer He is a male, and he is about seven feet five inches tall. It is a modifying predicate. It limits the scope of the murderer. People who meet this modification rule are suspects and need to be arrested and interrogated.

Advice: Arrested and interrogated. Advice is an action, that is, a piece of Java code. This Java code acts on those Joint points defined by point cut. In the same way, comparing to our example, the action of grabbing for interrogation acts on those who satisfy men. , people in a small county town in Java who are about seven feet five inches tall.

Aspect::Aspect is a combination of point cut and advice, so here we can make an analogy: "According to Lao Wang's clues, everything If a man with a height of seven feet five inches is found, he will be arrested and interrogated." This entire action can be considered an Aspect.

Finally, there is a diagram describing the relationship between these concepts:

Detailed explanation of Spring - AOP detailed explanation (AOP overview)3. Some other contents

Joinpoint in AOP can have many types: constructor method call, field setting and acquisition, method call, method execution, Exception handling and execution, class initialization. That is to say, in the concept of AOP, we can weave our custom Advice into the above Joinpoints, but not all the above joinpoints are implemented in Spring. To be precise, Spring only supports method execution type Joinpoints.

Type of Advice

before advice, advice that is executed before the join point. Although before advice is executed before the join point, it cannot prevent Execution of join point, unless an exception occurs (that is, in the before advice code, we cannot artificially decide whether to continue executing the code in the join point)

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

after throwing advice, advice executed when a join point throws an exception

after(final) advice, regardless of whether a join point exits normally or an exception occurs, it will be executed advice.

around advice, 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 objects to the original object Properties and methods.

The above is the detailed content of Detailed explanation of Spring - AOP detailed explanation (AOP overview). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template