Aspect-Oriented Programming of Spring AOP (1)

Source: Internet
Author: User

This article is for Reading Notes. For more information, see spring in Action 2 Chinese edition.

 

Of course, this article is also a boring explanation. Most of the explanations of AOP terms are not intuitive, so I will first copy the explanations in this book, then I add my own explanations, and then add a picture to add some explanations. In this way, I try to clarify these terms so that the subsequent explanations can be clearer.

Advice: The cut plane function is called "notification". A notification defines what the cut plane is and when it will be used. For example, should it reference before, after, before, and after a method is called, or only when the method throws an exception?

Joinpoint: A connection point is a point that can insert a cut surface during program execution. This can be used when a method is called, when an exception is thrown, or even when a field is edited.The code of the section can be inserted into the general process of the program through these points to add new behaviors.

Pointcut: it is not necessary to notify the program of all the connection points. The entry point can narrow down the connection points of the aspect notification. The entry point defines the "where" use of the Section. The definition of the start point matches one or more connection points to be woven.We usually use clear class and method names, or use regular expressions to define matching class and method name templates to specify these entry points. Some AOP frameworks allow creation of dynamic entry points, You can apply the section according to the running status (such as the method parameter value.

Aspect: This is a combination of notifications and entry points.The notification and entry point define all the content about the aspect-its function, and the time and place to complete this function.

Introduction: "Introduction" allows us to add new methods and attributes to existing classes. (I still don't quite understand this definition)

Target: the target is the object to be notified. It can be any written object or a third-party object. If you do not use AOP, this object can only process the main business logic (save data) and cross-transaction logic (log records) by itself ). With AOP, the two business logic can be separated. The target object is only responsible for the main business logic (Data Storage) of outbound traffic ). The logic (logging) of the Cross transaction is handed over to the "Notification" for processing.

Proxy: "proxy" is the object created after "notification" is applied to "target object. For us, the target object (the object before applying AOP) is the same as the proxy object (the object after applying AOP.

Weaving:"Woven"YesApply the sectionTarget objectComeCreate newOfProxy objectOfProcess.Section at the specified connection point (entry point)WovenTo the target object.The weaving process can take place at least three times in the lifecycle of the target object.:During compilation, class loading, and runtime.

Spring's support for AOP is proxy-based and limited to method injection,This is the way Spring AOP is woven into the plane. It is inRuntimeWhen the plane is woven, Spring AOP creates a proxy object to delegate it to the target object (proxy design mode ). Aspectj is based onCompile time, Aspectj 5 is based onClass Loading.

The following is my explanation of these terms. I will give a simple example.

Cross-transaction: If you save data and record logs are completed in the same method or in the same class, it can be said that record logs are cross transactions. 1. First record the data to the log, 2 ,,Save data, 3. Record the Save result after the SAVE is successful. 4. Record the exception information when an exception occurs. As shown in the following figure:

Notification (advice ):Cross transactions like this will be used in hundreds of places in the program, as shown in figure 1. These cross-transaction codes are very similar. Therefore, we can extract these cross-transactions so that our main business can process the main transactions, so that the cross-transaction will not affect my line of sight. In this way, both the code reuse effect and normal development of the program are not affected. Abstract transactions can be calledAdvice)". As shown in, the logtest class is a notification class.

In the preceding advice terminology, 1. The notification defines what the aspect is. As for the above logtest, our notification defines the aspect that records logs. 2. When to use it. For logtest, the three methods define the saveobject before the saveobject. After the saveobject, when the saveobject has an exception, of course, these three moments can be used at that time, or one or two of them can also be used. The notification is actually a cross transaction.Class.

Join point (joinpoint):Simply put, each business method in the main business logic is called togetherJoin point (joinpoint). For example, if the saveobject methods in the test class have the updateobject, deleteobject, and GetObject methods, these methods are used as the connection points. Think about it carefully.Method. We can add ourNotificationCode. As shown in figure 2, all
A diamond is called a connection point. These connection points are the methods used in the execution of the main program. As for how to join, I will introduce it later.

Pointcut):We add notifications to the primary service connection point (method), but it is impossible to add notifications to all connection points (methods) of the primary service. We will select one or more connection points to add notifications. In this way, the connection point is reduced. One or several connection points (methods) are called"Pointcut )".As shown in figure 2, the arrow in the middle indicates the starting point. It selects one or more methods to add them. For example, the saveobject method in the test method is a starting point. We can record the logs before, after, and when the data is saved. In this case, the explanation of "where" is displayed.

Aspect (aspect ):As mentioned in the preceding terminology, combining the notification and the entry point isSlice (aspect ).In my own words, a simple summary is: when the program is running, add the functions of the logtest class to the saveobject method of the test class.

Target ):It is the target object. Is the specified service object (TEST) to which the notification (logtest) is added ). As shown in, the test object is the target object. If AOP is not used, this test is used to record logs and save data. Use AOP to separate cross-businesses. The object that carries the main business isTarget object (target ).

Proxy):Proxy object. The above text also saysSpring's support for AOP is based on proxyThat is, after we add the notification to the target object, spring
AOP creates a proxy object for the target object. This proxy object will enclose the target object. When we apply the method in the notification to the method in the target object, we actually handed the method in the notification to the proxy object, the proxy object is then transferred to the target object. 3.

Weaving):I understand the process of adding a notification to the target object through a cut point and then creating a proxy for the target object.

 

 

There are still many shortcomings in the above explanation. I will make modifications to Spring AOP with a simple understanding.

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.