What is the AOP
The article will also be published on the official account of the same name, “Makesi”, a suitable official account of the student Party! Take you into the world of programming easy to understand!
1, concept,
In the software industry, 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.
Said the OOP
One day an interviewer asks you what is OOP?
And then you might say OOP with a look on your face, right? What? What the hell is this?! It’s basically Object Oriented Programming that you’ve been studying,
So how do we understand object-oriented programming?
We all know that before object-oriented programming there was procedural programming. The C language, the foundation of our programming, is procedural programming. How do you understand the difference?
Let’s take a simple example. When you want to eat an egg fried rice, what should you do? Here are two choices for you!
1. Buy your own ingredients, then cook the rice, scramble the eggs, add whatever ingredients you want and then cook them out.
2. You go to xyz hotel, sit down and shout, “Waiter! Have an egg fried rice.”
You see the difference! In fact, here should have a feeling, 1 is process oriented, 2 is object oriented!
So what's the difference between these two methods of programming?
The second way, you don’t need to know exactly how the egg fried rice is cooked, you just order it and you get it at the end. If you don’t want to eat after you order (before the waiter fires you, otherwise the boss might think you’re up to something and throw you out!) If you want to switch to beef fried rice, all you have to do is talk to the boss. And then if you’re the first one, the ingredients you just bought are useless. Since you only have eggs and no beef, you may have to start all over again! This is the most obvious difference between the two
Process orientation is step by step analysis, step by step implementation,
Object orientation is modeled, we just abstract out a class, wrap it up, and call it when we need it. As for the specific inside how to achieve (is how to copy the egg fried rice is not our business, we just need to eat it!
So now to summarize the pros and cons of both:
Object-oriented:
- Advantages: Easy maintenance, easy reuse, easy to expand
- Disadvantages: Poor performance, because the class invocation needs to be instantiated, high overhead, high resource consumption
Process-oriented:
- Advantages: Good performance
- Disadvantages: Not easy to maintain, reuse, and expand
Then back to our topic of the day, AOP for faceted programming! It’s based on OOP!
2. How to understand the section?
AOP is faceted programming, so what exactly does that aspect mean?
It’s easy to understand, a whole watermelon, we take a knife and cut it down the middle, and now we have two slices.
In programming, object to object, method to method, module to module are all facets
3. So what’s the use of facing the section?
Let’s start with a familiar example:
For example, now you have a Dog class and a Cat class. They both have a common method. They both run() and they both eat().
In our OOP, we’re going to write an Animal class that has the same methods as the Dog and Cat classes. The run () and eat ()
Then we just need to inherit from Animal when implementing Dog and Cat! This does reduce the amount of repetitive code we write.
But here’s the problem!
If there is a lot of duplicate code in the same place for multiple methods in our parent class at this point! What should I do? In addition, Java only provides single inheritance and cannot inherit from other parent classes.
OOP is a vertical system, top-down.
So here comes our hero! AOP is faceted.
Think of all the places where you need these common methods as one slice, and then isolate the common methods. And then cut across it.
And one might say, well, in our parent class, aren’t we just pulling out methods that are common to all of our subclasses? So isn’t this the same thing?
However, it is important to remember that the parent class also has its own actions (its own business code) mixed with the common methods of these subclasses, which can be very bloated! Becomes difficult to maintain!
AOP is designed to solve these problems!
AOP separates our so-called cross-cutting code from the original business code.
The difference between the two can be clearly seen in the picture above. In this process, you need to ensure that the original business code is not broken!
4. What problems does AOP solve?
Through the above analysis, it can be found that AOP is mainly used to solve: in the case of not changing the original business logic, enhance the cross-cutting logic code, fundamentally decouple, avoid cross-cutting logic code duplication.
5. Related concepts in AOP
Those of you who have studied Spring probably know that there are two very important things in Spring, one is IOC, and the other is AOP. IOC has been explained in my last article, and those who have not read it can check the history information. In Spring, AOP has many specialized concepts, listed below:
Aspect
(Aspect) : An Aspect declaration is similar to a Java class declaration in that it contains pointcuts and Advice.Joint point
Join point: represents a point clearly defined in a program, typically including method calls, access to class members, execution of exception handler blocks, etc. It can also nest other joint points.Pointcut
Pointcuts: Represents a set of joint points, either combined through logical relationships or grouped together through wildmatches, regular expressions, etc., that define where the corresponding Advice will take place.Advice
(Enhanced) : Advice is defined inPointcut
It defines the specific operation to be done by the program point, which is distinguished by before, after, and around to execute the code before, after, or instead of each joint point.Target
(Target object) : WovenAdvice
The target object of.Weaving
(woven) : willAspect
Connect to other objects and createAdvice
Object process
Then finally take an example, the example is seen above CSDN, very image! The text is a little bit much, but I still hope you read it, after reading really benefit
Let us suppose, once upon a time, there was a small county called Java. On a dark and windy night, a murder happened 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.
What is the correspondence between story and AOP?
First of all, in Spring AOP, the Joint point refers to the execution point of all methods, and the point cut is a description that refers to the Joint point. We can then determine which Joint points can be woven into Advice. As a simple analogy to our example above, Joint Point is the equivalent of a small town in Java, and Pointcut is the equivalent of Lao Wang’s accusation that the killer was 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?
Joint point
People of small towns in Java: Because by definition,Joint point
It’s all possible to weave inAdvice
In Spring AOP, all method execution points can be considered to beJoint point
In our example, the murder took place in a small county, and everyone in that county could be a suspect.Pointcut
: Male, about 7 ‘5 “: We know that all methods (joint point) can be wovenAdvice
But we don’t want to weave in all methodsAdvice
And thePointcut
The JoinPoint () function provides a set of rules to match joinPoints and adds rules to joinPoints that meet the rulesAdvice
Similarly, no matter how stupid the magistrate was, he knew that he could not arrest all the people in the county for questioning. 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
: Brought in for questioning,Advice
Is an action, that is, a piece of Java code that applies to the ones defined by point CutJoint point
Similarly, in contrast to our example, the act of grabbing and questioning was applied to the men of the small town of Java, who were about seven feet and five inches tall.Aspect
: :Aspect
Is the point cut withAdvice
The whole action of “According to Lao Wang’s clue, every male found seven feet and five inches tall should be arrested and interrogated” could be considered as oneAspect
.
So this little story actually shows us a lot about our relationship!
To see the students here, I think perseverance is quite good! Because this article is basically theoretical, LAN thought it would be very boring to write in the process of writing and planning. But no way, we can read it to grasp the essence of it, that is enough!
Finally, add AOP application scenarios
The Authentication authority
Caching cache
Context passing
Error handling Error handling
Lazy loading
Was Debugging Debugging
Logging, tracing, Profiling and Monitoring record tracking optimized calibration
Performance optimization
Persistence Persistence
Resource pooling Resource pools
Synchronization synchronous
The Transactions transaction
END
Finally, I hope this article can help you. If there are any mistakes in the article, please correct them!