[Spring Study Notes] [6.1 AOP-based AOP]

Source: Internet
Author: User

6.1.1 what is AOP

Consider the following problem: some services in the system need to be logged. For example, the payment service in the payment system needs to record the payment logs, which may be quite complicated for the payment system, for example, you may have your own payment system or a third-party payment platform. How can you solve this problem?

  • Traditional solutions:

1) in the log section, the common class LogUtils is used to define the "longPayBegin" method to record the payment start log. The "logPayEnd" method is used to record the payment result:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1613595T5-0.JPG "/>

2) In the payment section, define the IPayService interface and the payment method "pay". Two implementations are defined: "PointPayService" indicates point payment, and "RMBPayService" indicates RMB Payment; and in each payment implementation, the payment logic and logging are as follows:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1613592230-1.JPG "/>

3) The payment implementation obviously has repeated code. This repetition can obviously be eliminated using the template design pattern:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/16135954A-2.JPG "/>


4) at this point, we have designed a reusable interface. But do you think it will be good to record logs? Is there a better solution?

What should we do if we add a statistical function to the points payment method, such as recording the total points of users and the number of points currently consumed during payment? Directly modifying the source code to add logging completely violates one of the most important object-oriented principles: the open and closed principle is open to extensions and closed to modifications )?


  • Better solutions: Because the log component is used in our payment component, that is, the log module is cross-cutting to the payment component, it is difficult to separate the log component from the traditional program design, that is, our payment component is not coupled; therefore, Aspect-Oriented Programming AOP is born, it can separate our components, so that the components are completely not coupled:

1) with Aspect-Oriented Programming, our payment component looks as follows, and there is no longer anything about the log component in the Code;


650) this. width = 650; "class =" center "src =" http://www.bkjia.com/uploads/allimg/131228/1613593925-3.JPG "/>

2) Therefore, the log is extracted to a specific aspect. The AOP implementer will route the log function to our payment component at the right time to completely decouple the payment component and log component.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/16135a4V-4.JPG "/>

You may not understand this. It doesn't matter. Let's look at it first.



Aspect-oriented programming (AOP): Also known as Aspect-Oriented Programming, is a programming paradigm that provides an improved Object-Oriented Programming (OOP) by taking program structures into account from another perspective ).


During OOP development, components are developed based on components such as classes and then combined. The biggest problem with OOP is that components cannot be decoupled for development. For example, the above example is, AOP is designed to overcome this problem, and is used to separate the coupling.

AOP provides developers with a mechanism for separating and weaving the cross-cutting concerns, such as the log concerns that cross-cutting the payment concerns), separating the cross-cutting concerns, and then weaving them into the system through some technology, this completes our functions without coupling.

6.1.2 what can I do

AOP is mainly used for separation and weaving of cross-concern points. Therefore, you need to understand cross-concern points and weaving:

  • Focus:It can be considered as anything of interest, such as the above payment component;

  • Separation of concerns:Refine the problem to separate parts, which can be understood as an inseparable component, such as the preceding log component and payment component;

  • Cross-cutting concerns:One component cannot complete the required functions and must collaborate with other components, such as log components cross-cutting over payment components;

  • Woven:After the separation of the cross-cutting concerns, you need to use a certain technology to integrate the cross-cutting concerns into the system to complete the required functions. Therefore, you need, weaving may take place during the compilation, loading, and runtime.

There may be many cross-cutting concerns, such as non-business basic functions such as logs, transaction processing, caching, performance statistics, and permission control. It may also be business: for example, a service component is cross-sectional with multiple modules. 6-1

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1613595128-5.JPG "/>

Figure 6-1 concerns and cross-cutting concerns


Traditional payment methods:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1613592N4-6.JPG "/>


In the aspect-oriented approach, the cross-cutting concerns are separated first, and then the cross-cutting concerns are woven into the payment system:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1613595647-7.JPG "/>

What can AOP do:

  • It is used to separate the cross-concern and weave the cross-concern to the system; for example, the log mentioned above;

  • Improve OOP;

  • Reduces coupling between components and modules;

  • Make the system easy to expand;

  • In addition, due to separation of concerns, you can obtain better reuse of components.

6.1.3 basic concepts of AOP

Before performing AOP development, familiarize yourself with the following concepts:

  • Connection Point Jointpoint):Indicates the extension point of the cross-cutting concern to be inserted in the program. The connection point may be class initialization, method execution, method call, field call, or Exception Handling. Spring only supports the method execution connection point,InAOP"Where to do";

  • Pointcut):Select a set of related connection points, that is, the set of connection points. Spring supports the perl5 Regular Expression and AspectJ entry point mode. Spring uses the AspectJ syntax by default,InAOP"Where to do the set";

  • Notify Advice):For the behavior executed on the connection point, the notification provides a means to expand the existing behavior at the connection point selected by the start point in AOP; including the pre-notification before advice) post-notification (after advice) and surround notification around advice), implement AOP in Spring in the proxy mode, and link notifications to the interceptor that wraps around the connection point through the interceptor mode;InAOPIs "what ";

  • Aspect/Aspect):Modularization of cross-cutting concerns, such as the log component mentioned above. It can be considered a combination of notifications, introductions, and entry points. In Spring, Schema and @ AspectJ can be used for organization implementation;InAOP"Where to do and what to do ";

  • Introduce inter-type declaration):This is also known as the internal Type Declaration. To add additional fields or methods to existing classes, Spring allows the introduction of new interfaces to correspond to an implementation) to all target objects of the proxy objects ),In AOP"What to introduce )";

  • Target Object):The object to be woven into the cross-concern, that is, the object selected by the starting point, the object to be notified, which can also be called the "notified object "; since Spring AOP is implemented in the proxy mode, this object will always be a proxy object,InAOP"Who to do";

  • AOPProxy AOP Proxy):The AOP framework uses the objects created in the proxy mode to insert a notification at the connection point, that is, the application plane.Apply a plane to the target object through a proxy. In Spring, the AOP proxy can be implemented using JDK dynamic proxy or CGLIB proxy, and the Application Section is applied through the interceptor model.

  • Weaving):Weaving is a process that applies a plane to a target object to create an AOP proxy object. It can be performed during the compilation, class loading, and runtime.

In AOP, select the connection point of the target object through the starting point, and then weave the notification at the corresponding connection point of the target object, while the starting point and notification are cross-section concerns ), the implementation of the application plane at the target object connection point is through the AOP proxy object, as shown in 6-2.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/16135931I-8.JPG "/>

Figure 6-2 conceptual relationship

Next, let's take a look at the notification types in Spring:

  • Frontend notification Before Advice):A notification that is executed before the method at the connection point selected by the start point. This notification does not affect the normal program execution process. Unless the notification throws an exception, the exception will interrupt the execution of the current method chain and return ).

  • Post notification After Advice):Notifications executed after the method at the connection point selected by the start point include the following types of post notifications:

    • Post-return notification After returning Advice):The notification that is executed when the method at the connection point selected by the start point is executed normally. The post-notification is called only when the method at the connection point does not throw any exceptions and returns normally.

    • After throwing Advice):The notification is executed when the method at the connection point selected by the start point throws an exception and returns the exception. The exception notification is called only when the method at the connection point throws any exception and returns the exception.

    • Post-final notification After finally Advice):The notification that is executed when the method returned at the connection point selected by the start point is executed, regardless of whether an exception is thrown or not, similar to the finally block in Java.


  • Surround notification Around Advices):Notifications that are executed by the method at the connection point selected by the start point can be customized before and after the method call, it also determines whether to execute the method at the connection point, replace the return value, and throw an exception.

The positions of various notification types in the UML sequence diagram are 6-3:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1613594633-9.JPG "/>

Figure 6-3 Notification Type

6.1.4 AOP proxy

The AOP proxy is the object created by the AOP framework in proxy mode. Spring uses JDK dynamic proxy or CGLIB proxy. Spring uses JDK dynamic proxy by default, so that any interface can be used as a proxy, if the object implemented by the proxy is not an interface, CGLIB proxy is used by default. However, CGLIB proxy can also be applied to the interface.


AOPThe purpose of the proxy is to weave the aspect into the target object.


The concepts are all over. Let's take a look at the HelloWorld of AOP! Right.


This article from the "CEO Road" blog, please be sure to keep this source http://zhaohaibo.blog.51cto.com/7808533/1285947

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.