Spring AOP Glossary

Source: Internet
Author: User
Transferred from: 1. All I know about AOP

   Looking at AOP, there are a lot of terms, and there is also a pull style name. For Aspect-oriented programming, it is a useful supplement to OOP and so on. Suddenly you are at a loss, thinking: No wonder many people tell me how difficult it is to use AOP. After reading it, I found out that it is an unpretentious Application Based on Java, including IOC, and many such terms.

2. Why does AOP apply?

   1. For convenience, a well-known foreign master said that programmers are "lazy" because he has done everything he has done for the program. With AOP, you can write much less code. This is enough.
   2. for a clearer logic, you can focus on your own business without thinking about other things, including security and things, logs.
 Nbsp; 3. AOP terms
   When I first read so many terms, it was not easy to accept at once. I will understand them soon.
   1. Notification (advice)   
      It is the function you want, that is, the security, things, logs, and so on. You can define a role first, and then use it where you want to use it.
  2. Join point (joinpoint)
   This is a better explanation, that is, the place where spring allows you to use notifications. That's really much more. Basically, before and after each method (either of them works ), it can be a connection point when an exception is thrown. Spring only supports method connection points. other operations, such as aspectj, can be performed during constructor or attribute injection. However, this is not a concern. Remember that all operations related to methods (throws an exception) are connection points.
  3. pointcut)
   Based on the connection points mentioned above, you can define the starting point. If there are 15 Methods in your class, there will be dozens of connection points, however, you do not want to use notifications near all methods (call them for weaving and later). You just want to make a few of them, before calling these methods, if you do something later or throw an exception, you can use the cut points to define these methods, let the cut points filter the connection points, and select the methods you want.
  4. aspect)
   Aspect is a combination of notifications and entry points. Now we can see that there is no connection point. The connection point is designed to help you understand and understand the concept. The notification explains what to do and when to do (you can know when to use before, after, and around in the method name), and the starting point shows where to do it (specify the method in the end ), this is a complete section definition.
  5. Introduction (Introduction)
   Allow us to add new method attributes to an existing class. Does this mean that the Section (that is, the new method property: Notification defined) is used in the target class?
  6. Target)
   The introduced target class, that is, the object to be notified, that is, the real business logic, can be woven into the aspect without knowing it. Focus on the business logic.
  7. Proxy)
   How to Implement the entire AOP mechanism is through proxy, which will be explained later.
   8. Weaving)
   Apply the aspect to the target object to create a new proxy object. There are three methods: Spring uses runtime and why is runtime, which will be explained later.
The key is: the cut point defines which connection points will be notified.

 4. I understand the principles of AOP.

   Spring uses the proxy class to wrap the plane surface and weave them into the bean managed by spring. That is to say, the proxy class is disguised as the target class, which intercepts 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 disguised class will first execute the section, then forward the call to the real target bean.
   Now, you can think about how to create this camouflage class, so that it will not be detected by the caller (through JVM check, Java is a strong type check, where all types should be checked ).
   1. implement the same interface as the target class, and I also implement the same interface as you. The upper layer is called at the interface level anyway, in this way, I pretend to be the same class as the target class (the same interface is implemented, and I am a brother), So I escaped the type check. At the Java runtime, with the post-binding of polymorphism (So spring uses runtime), the disguised class (proxy class) becomes the real implementation of the interface, and it contains the real target class, the final implementation of specific functions is the target class, but the disguise class did something before (writing logs, security checks, and transactions ).
   This is like a person asking you to do something. At this time, your brother will come out first. Of course, he will not be able to come out and think it is you, although your younger brother cannot do this, he knows that you can do it, so he agreed, and received some gifts (write logs) to complete the gift, I have done something for someone else, so your brother has come to you again. Finally, I have done it myself. But you don't know that your brother has already received a gift. You just want to focus on doing this well.
   
   Following this idea, if this class does not implement an interface, how can you disguise me and I have no chance to let you create this twin brother, in this way, we can create a subclass of the target class in 2nd proxy methods, and create a son to disguise my son.

   2. generate a subclass call. This time, the subclass is used as a camouflage class. Of course, this can also escape the JVM strong type check. Can I inherit it? Of course, I cannot find it. The subclass overrides all methods of the target class, of course, in these rewriting methods, not only the functions of the target class are implemented, but also some other functions (such as writing logs, security checks, and transactions) are implemented before these functions ).
  In this comparison, my son learned everything from his father. Everyone asked his son to do things, but before every time his son did the same things as his father, you need to receive a small gift (write logs) before doing the real thing. Of course, I don't know what my son did. There is one thing to say here: some of my skills are unique to my father (Final). If my son cannot learn them, he will not be able to do it, naturally, you cannot receive gifts.

  In the previous sibling mode, spring uses JDK Java. lang. reflect. proxy class, which allows spring to dynamically generate a new class to implement the necessary interfaces, weave notifications, and forward any calls to these interfaces to the target class.

   In the next parent-child mode, spring uses the cglib library to generate a subclass of the target class. When this subclass is created, spring binds the notification, and delegate the call to this subclass to the target class.
   In contrast, the sibling mode is better. He can achieve loose coupling better, especially when he is shouting interface-oriented programming today, the Parent-Child mode only does not implement interfaces, notifications can also be woven. An exception should be made.

A summary of the terminology and principles of AOP for beginners

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.