Spring AOP concept understanding (easy to understand)

Source address: http://www.verydemo.com/demo_c143_i20837.html


  1. AOPs I know

  At first glance, aop is a lot of terms, and there is also a fancy name, aspect-oriented programming, which is said to be a useful supplement to OOP and so on. Suddenly you are overwhelmed, thinking: No wonder many people tell me how difficult aop is. When I looked into it, I found out that it is some simple and unpretentious applications based on java, including ioc, including many such terms, which are all the same.

  2. Why use aop

  1 Just for convenience, I saw a famous foreign master say that programmers are "lazy people" because they let the program do everything they do. Using aop can save you writing a lot of code, which is enough.

  2 is for clearer logic, allowing your business logic to focus on your own business, without thinking about other things, including: security , things, logs, etc.

 3. Those aop terms

  At first glance, so many terms are not easy to accept. Take your time and you will understand soon.

    1. Advice

  It is the function you want, that is, the  security , things, logs, etc. mentioned above. You define it first, and then use it where you want to use it.

    2. JoinPoint

  This is a better explanation. It is where spring allows you to use notifications. There are really many. Basically, the front and back of each method (both can also be used), or when an exception is thrown, can be a connection point. Spring only supports method connection points. Others such as aspectJ can also let you do it in constructor or property injection, but that's not what we are concerned about, just remember that the front and back (throwing exceptions) related to methods are all Junction.

    3. Pointcut

  Based on the connection points mentioned above, to define pointcuts, if you have 15 methods in a class, there are dozens of connection points, but you don't want to use notifications around all methods (using It is called weaving, and we will talk about it later), you only want to let a few of them do something before calling these methods, after or when an exception is thrown, then use pointcut to define these methods, let pointcut do something To filter the connection points, select the methods you want.

    4. Aspect

  Aspects are a combination of advice and pointcuts. Now I found it, there is nothing to connect the points, the connection points are for you to understand the pointcut better, and you can figure it out, just understand the concept . The notification explains what to do and when to do it (when you can know it through the before, after, around, etc. in the method name), and the pointcut explains where to do it (specify which method it is), which is a complete aspect definition .

    5. Introduction

  Allows us to add new method properties to an existing class. Isn't this the application of the aspect (that is, the new method attribute: the definition of the advice) to the target class?

    6. target

  The target class mentioned in the introduction, that is, the object to be notified, that is, the real business logic, can be woven into the aspect by us without knowing it. Instead, focus on the logic of the business itself.

    7. Proxy

  How to realize the whole set of aop mechanism is through the proxy, which will be explained in detail later.

    8. Weaving

  The process of applying an aspect to a target object to create a new proxy object. There are 3 ways, spring uses runtime, why is runtime, explained later.

  The point is: pointcuts define which join points will be notified

  4. The principle of aop as I understand it

  Spring wraps aspects with proxy classes, weaving them into Spring-managed beans. That is to say, the proxy class is disguised as the target class, it will intercept the call to the method in the target class, so that the caller's call to the target class will first become the call disguise class, and the aspect will be executed first in the disguise class, and then the call will be forwarded. to the real target bean.

  Now you can think for yourself, how to come up with this disguised class so that it will not be discovered by the caller (after the JVM inspection, JAVA is a strong type check, and the type must be checked everywhere).

  1. Implement the same interface as the target class, I also implement the same interface as yours, anyway, the upper layers are all interface-level calls, so I pretend to be the same class as the target class (implementing the same interface, we are brothers ), it also escaped the type check. When the java runtime uses polymorphic late binding (so spring uses runtime), the camouflage class (proxy class) becomes the real implementation of the interface. The real target class is wrapped, and the target class finally implements the specific function, but the camouflage class has done something before (writing logs, security checks, things, etc.).

  It’s like if someone asks you to do something, every time your brother comes out first, of course he can’t tell the difference, thinking it’s you. Although your brother can’t do it, he knows you can. , so I agreed, and I received some gifts (writing a journal), and after I received the gifts, I did things to others, so your brother came to your brother again, and finally decided whether this was done or not. yourself. But you don't know that your brother has received the gift, you just concentrate on doing it well.

  Thinking along this line of thought, if the class itself does not implement an interface, how can you disguise me, I have no chance for you to make this twin brother, then use the second proxy method to create a target class Subclass, have a son, let the son disguise me

  2. Generate a subclass call. This time, the subclass is used as a camouflage class. Of course, this can also escape the strong type check of the JVM. Do I inherit it? Of course, I can't find it out. The subclass rewrites all the target class. Methods, of course in these overridden methods, not only implement the functions of the target class, but also implement some other functions (writing logs, security checks, things, etc.) before these functions.

  The comparison this time is that the son first learned all his skills from his father, and everyone asked his son to do things, but every time the son did the same thing as his father, he had to receive a small gift (write a journal), and then to do real things. Of course the father didn't know what his son did. There is one thing to say here, some skills are unique to the father (final), if the son can't learn it, he can't do it if he can't learn it.

  In the former sibling mode, spring will use the JDK's java.lang.reflect.Proxy class, which allows Spring to dynamically generate a new class that implements the necessary interfaces, weaves in advice, and forwards any calls to those interfaces to the target kind.

  In the latter parent-child model, Spring uses the CGLIB library to generate a subclass of the target class. When creating this subclass, Spring weaves the notification and delegates calls to this subclass to the target class.

  In contrast, the brother mode is better, it can better achieve loose coupling, especially in the case of interface-oriented programming today, the parent-child mode can only weave notifications when the interface is not implemented, it should be Make an exception.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325663072&siteId=291194637
Recommended