Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to talk about AOP in spring in the most popular way

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to use the most popular way to talk about AOP in spring. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

The purpose of writing this series (you can skip it)

I wrote this series because I was a stupid person. I once suspected that my IQ was not suitable for the programming industry. Because in the process of self-study, I have read countless articles, blogs and experiences of others. But that lot of terminology, technical basis, it is difficult to read and understand. Coupled with the intermingling of good and bad online articles, many articles are written for people who already understand. An example: Q: what is control reversal? A: the control of a built-in object in its own object is reversed. After the inversion, the creation of the built-in object is no longer controlled by its own object, but by a third-party system. ??? In my opinion, this professional explanation is not for people who learn, but for people who already have considerable experience. Can't you explain these things in a more emotional way? As a language, I think programming language is quite emotional to some extent. Since we can understand each other when we talk to each other, these technologies can be explained in a perceptual way to a certain extent.

AOP

AOP is oriented to the aspect. Well, I said it for nothing. What is facing the section? This is a question, but let's not rush to answer it.

one。 For instance

Most of our code structure is MVC mode. Let's use MVC as a simple example. In MVC mode, the most commonly used spring + springMVC + Mybatis looks like the following.

┌─┐ | view | user interface ├─┤ | Controller | Control layer ├─┤ | Service | Service layer, also known as business layer ├─┤ | Dao | persistence layer Also known as data access layer ├─┤ | Database | Database └─┘

In addition, Model is used as a data carrier to pass from layer to layer as long as you have come into contact with JavaWeb development, you can understand it. If you don't understand this, don't delve into this knowledge for the time being, first focus on framework application.

This Service is a UserService that serves only one purpose: to save the user.

┌─┐ | view | ├─┤ | Controller | ├─┤ | UserService.add | ─── > New user ├─┤ | Dao | ├─┤ | Database | └─┘

Now, there is a need for us to add logs to Service, the colleague who developed this function is gone, and the manager asked us to do this requirement. Then according to the concept of object-oriented. Call a log method before calling the service method, and call a log method after the call, then add two lines of code to the code. So the code looks like this:

┌─┐ | view | ├─┤ | Controller | ├─┤ | log.info... | | ─── > call log | UserService.add | ─── > New user | log.info... | | | ─── > call log ├─┤ | Dao | ├─┤ | Database | └─┘ |

Is that correct? The answer is of course correct, the demand has been solved, the log has been saved normally, and the leader is very happy.

The next day, there was another need to control service's transaction, OK, and the code became like this.

┌─┐ | view | ├─┤ | Controller | ├─┤ | Transaction | ─── > transaction Management | log.info... | | ─── > call log | UserService.add | ─── > New user | log.info... | | | ─── > call log | Transaction | ─── > transaction management ├─┤ | Dao | ├─┤ | Database | └─┘ |

On the third day, the project manager asked us to record the time it took for the method to run, and then the code looked like this.

┌─┐ | view | ├─┤ | Controller | ├─┤ | beginTime | ─── > start time | Transaction | ─── > transaction management | log.info... | | ─── > call log | UserService.add | ─── > New user | log.info... | | | ─── > call log | Transaction | ─── > transaction management | endTime | ─── > end time ├─┤ | Dao | ├─┤ | Database | └─┘ |

On the fourth day, the customer asked us to implement the permission verification when adding new users, so the code became like this.

┌─┐ | view | ├─┤ | Controller | ├─┤ | permissions | ─── > Verification permission | beginTime | ─── > start time | Transaction | ── ─ > transaction Management | log.info... | | ─── > call log | UserService.add | ─── > New user | log.info... | | | ─── > call log | Transaction | ─── > transaction management | endTime | ─── > end time ├─┤ | Dao | ├─┤ | Database | └─ | ──┘ / * I don't know what you think. But now, I still think this way of writing is quite good. It is easy to understand and so readable that I don't know where it is. Of course, there is a premise that this is the only way for this project. * /

On the fifth day, the colleague who developed UserService came back and looked confused. There was only one line when he left, but now there is a lot of code that doesn't know what it means. Come on, he developed on this basis and added various methods and functions. As the demand changes, slowly, a hundred service become like the above. Then the logging function changes, you find that it will affect the calling class, search the entire project, and find that this method has been called in thousands of places. Ten thousand grass and mud horses swept through your mind, and then you offered to resign. Then a newcomer took over the project and opened it. What was the big piece of shit that came into view? Bravely changed several editions, each version has BUG, the leader was angry, the project was cancelled, a lot of money found several people to do it again.

Then there is a problem. If the demand is messed up, you can resign, and if the project is bad, you can do it again. So how did the first person in the world encounter this problem solve it? Technology is this technology, won't you still encounter these problems if you do it again? Then how can I expand the function? Will all projects end up in this desperate situation? So predecessors began to explore:

┌─┐ | Controller | | ┼─ > extension method is written here? ├─┤ | Service | ├─┤ | ┼─ > extension method is written here? | Dao | └── ─┘

Isn't it all the same thing? It doesn't solve the problem at all.

I can't solve it for ordinary people, but there are plenty of people in the world with their own plug-ins. As a result, a few people found a solution! Since I want to expand the method, I have to write code in your method, so can I expand it without changing your code?

For example, in this place!

┌─┐ | Controller | ├─┼─ > for example, write here! | Service | └─┘

What the hell? Are you asking me to write code in a trumped-up place?

Of course not. The Great God means that the code should be written like this.

┌─┐ | Controller | ┌─┐├─┼─┤ log.info... | | Service | └─┘└─┘ |

But when it is executed, it will be like this.

┌─┐ | Controller | ├─┤ | log.info... | | Service | └─┘ |

What the hell?? Why is that?

The answer is simple: first of all, since you are going to know AOP, you should have some Java basics. We all know that Java files are useless, which is equivalent to a txt file. When the program is actually running, the Java file is compiled into a class file through the compiler to run. So we seem to see the point, compiler! Since the final run is the class file, and the class file is generated by the compiler, can we tamper with the compiler? Yes, to achieve the above effect, you need a different compiler. Compile the log code into service, of course, this compiler does not need us to develop, but has already been done. (if I could develop it, I would have given a speech at the Google developer conference.) this is famous: = = ajc compiler = = (he belongs to the AspectJ framework, you can use this framework alone to write an example, you can deepen your understanding.)

1. Imagine, There are two things like ┌─┐ | Controller.java | ├─┤ ┌─┐ | Service.java | | log.info.java | └─ ───┘ └─┘ 2. Then start compiling: the compiler begins to weave the code in Service.java into a class file. 3. Ajc compiler starts compiling: ajc starts inserting log.info.java weaving into service. ┌─┐ | Controller.java | _ ├─┤ _ / log.info.java | | Service.java |\ _ _ | └─┘ 4. Like above, weave log.info into this method. 5. The resulting calss file is like this: ┌─┐ | Controller | ├─┤ | log.info... | | Service | └─┘ |

Yes, the problem is solved. With this compiler, the previous problem can be easily solved. = = ps: ajc compiler is not the only solution =

So here comes the question again, what is the aspect? maybe you already have the answer.

┌─┐ | Controller | ┌──┐├─┼─ > | We write code from this point of view, that is, facing the aspect. | | Service | | ├─┼─ > | We write code from this point of view, that is, facing the aspect. | | Dao | └──┘└─┘ |

We stand at the angle of that line to write code, that is, facing the section. Does that line divide the original code into two parts? A little more vivid.

┌─┐ | Controller | └─┘──, is it like a line that cuts the whole code? So this line It is called section ──, such as: log call ──, such as call time ──── ─ such as: transaction Management ┌─┐ | Service | ├─┤ | Dao | └─┘

Each of the above, for example, is a facet class, and it is also equivalent to a facet. After all, no one stipulates that we must have only one facet, right?

Aspect-oriented is a solution when people face difficult problems, it is not a new language or a new technology. He eliminated a series of bad habits of object-oriented programming. In other words, it solves some shortcomings of object-oriented.

At this point, I believe some people still don't quite understand the meaning or function of aspect. It doesn't matter, don't be discouraged, you must be much smarter than I was then. Please read on.

This picture may not be an intuitive expression of how important it is to face the aspect. ┌─┐ | Controller | └─┘ ─ ┌─┐ | Service | ├─ ─┤ | Dao | └─┘ then What about this picture? ┌─┬─┐ | Controller1 | Controller2 | Controller3 | Controller4 | Controller5 | Controller6 | └─┴─┘─── The aspect-oriented method of ─ Can be woven into all service There is no need to change the code of service. ─── permissions check ───. ─── log calls ─ ── call time ──── ─── transaction Management ──── ┌─ ─┬─┐ | Service1 | Service2 | Service3 | Service4 | Service5 | | Service6 | └─┴─┘─ | The aspect-oriented method of ─ Can be woven into all Dao There is no need to change the code of Dao. ─── log calls ───. ─── call time ─── ─ ┌─┬─ ─┐ | Dao1 | Dao2 | Dao3 | Dao4 | Dao5 | Dao6 | └─┴──── ─┴─┘, I just need to declare a class The method in this class can be inserted into the location of any class

Some people say that a series of programming problems mentioned above can be solved by using proxy mode. That's right! Aspect-oriented ─── is the idea. The agent mode ─── is the realization of the idea.

two。 Draw a picture.

It's been painted on it.

three。 Write a code.

Only talk about concepts, and then we will talk about examples.

four。 Several major concepts of speaking personally. Section (Aspect):

Extended class concept: Aspect declarations are similar to class declarations in Java, including some pointcuts and corresponding enhancements in Aspect. Human words: create a class that will be inserted into the code.

two。 Connection point (Joint point):

Call point concept: represents a clearly defined point in a program, typically including method calls, access to class members, and execution of exception handler blocks, etc., and can nest other joint point itself. Human words: the included content, such as the pointcut is the userService.add () method: the join point is userService.add (). If the pointcut is all service under the com.xxx package: any one of the service methods under the com.xxx package, for example, the pointcut is the method at the beginning of all the add under the com.xxx package: the method at the beginning of all add under the com.xxx package

3. Tangent point (Pointcut):

Where to enhance the concept: represents a set of joint point that are combined either by logical relationships or by wildcards, regular expressions, etc., and define where the corresponding enhancements will occur. Or where we want to weave it: where to insert all the service methods under the userService.add () method or the com.xxx package or all the add-beginning methods under the com.xxx package

4. Enhancement (Advice):

Extended method concept: Advice defines the specific operations to be done at the program points defined in it, distinguishing between before, after, or instead of executing code through before, after, and around. Human words: what needs to be defined in the aspect class, the extension before the call of the join point method, after the extension, or before and after the extension.

5. Target object (Target):

Enhanced goal concept: enhanced (Advice) target object.. Human words: an enhanced goal

6. Weave (Weaving):

Inserted action concept: the process of connecting Aspect to other objects and creating an Adviced object. Human words: insert the facet class into it

For example, we need to add log ┌─┐ to the add method of service | Controller | └─┘──

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report