Understanding of aspect-oriented programming (AOP)

Source: Internet
Author: User
Tags aop reflection regular expression

In traditional writing business logic processing code, we usually do a few things: logging, transaction control and permission control, and then write the core business logic processing code. When the code is written and looked back, I can't help but see that in hundreds of lines of code, it's really a few lines of core business logic, as shown in Figure 6-4. Method of complex method, class complex, such a son with helpless regret to spend the number of spring and autumn. If, at the end of the project, a sudden decision is made to make big changes in the control of authority, tens of thousands of methods will have to pay one by one "visits" and suffer "worse".

If you can extract all the common code from many of the methods in Figure 6-4, place it in a centralized management, and then dynamically weave the common code into the container at the very least two questions:

Java EE programmers in the writing of specific business logic processing methods, only care about the core business logic processing, not only improve the efficiency, but also make code changes concise and elegant.

In future maintenance, because business logic code is stored separately from common code, and common code is centrally stored, it makes maintenance easier and easier.

Aspect-oriented programming AOP technology is to solve this problem is born, the section is cross-section, as shown in Figure 6-5, represents a universal common function, for example, log section, permission section and transaction plane.

The following is an example of the AOP implementation process (see Figure 6-6) userservice The user-managed business logic component, which provides an in-depth analysis of the implementation principles of AOP technology. AOP technology is based on the reflection mechanism and dynamic proxy mechanism of Java language. The business logic component is running, the AOP container dynamically creates a proxy object for the consumer to invoke, which has been successfully cut into the connection point of the target method by the Java EE Programmer's intent, thus enabling the function of the cut and the function of the business logic to execute simultaneously. In principle, the caller directly invokes the proxy object that the AOP container dynamically generates, and then the proxy object invokes the target object to complete the original business logic, while the proxy object has synthesized the slice and the business logic method.

Some of the concepts covered in Figure 6-6 are explained below.

Slice (Aspect): In fact, is the realization of the common function. such as log section, permission section, transaction plane, etc. In practice, it is usually a common Java class that holds common function implementations and is specified in the configuration because it can be identified by an AOP container as a slice.

Notice (Advice): Is the concrete implementation of the plane. To the target method as the point of reference, according to place different places, can be divided into the front notice (before), post notice (afterreturning), abnormal notice (afterthrowing), Final Notice (after) and surround notice (Around) 5 kinds. In practice, it is usually a method in the slice class, which is the type of notification, and is also specified in the configuration.

Connection point (Joinpoint): Is the location where the program is able to insert slices during operation. For example, method calls, exception throws, or field modifications, but spring supports only method-level connection points.

Pointcut (Pointcut): Used to define which connection points the notification should be cut into. Different notifications usually need to be cut into different connection points, and this precise match is defined by the regular expression of the pointcut.

Target object: Is the object that is about to cut in, which is the object being notified. All of these objects have been left with clean core business logic code, and all the common functionality code waits for the AOP container to cut in.

Agent object (proxy): An object that is created dynamically after the notification is applied to the target object. It can be simply understood that the function of the proxy object is equal to the core business logic function of the target object plus the common function. The proxy object is transparent to the user and is the product of the process of the program running.

Weaving (weaving): The process of applying a slice to a target object to create a new proxy object. This process can occur in the compile period, class loading period and running period, of course, different occurrence points have different prerequisites. For example, in the case of a compile period, requires a special compiler that supports this AOP implementation; In the class loading period, there is a special class loader that supports AOP implementation, which can be dynamically implemented directly through the reflection mechanism and dynamic agent mechanism of Java language.

The following are supplementary:

AOP is the abbreviation of Aspect oriented programming, which means that it is a kind of technology to implement the unified maintenance of program function by precompilation and runtime dynamic agent.

AOP and OOP are two kinds of design ideas in different fields.

OOP (object-oriented programming) encapsulates the entities and their attributes and behaviors of the business process to achieve clearer and more efficient logical unit partitioning.

AOP is the extraction of the section in the process of business processing, which is confronted with a step or stage in the process of processing, to obtain the isolation effect of the low coupling between the parts of the logic process.

We can simply understand AOP and oop from the literal above, and we can't use the following as an understanding:

OOP is actually an encapsulation of the object's properties and behavior, AOP does not talk about this, but AOP deals with a step and a stage from which to extract the slices, that is, if there are repetitive actions in several or more logical processes, AOP can be extracted, using dynamic proxies, Realize the unified maintenance of the program function, so it may be too subtle, if you say that the right to judge, logging, etc., may be understood. If we simply use OOP, what about permission judgments? Do you have permission to judge before each operation? What about log records? Manually add a log to the beginning, end, and exception of each method? All, if you can use AOP to do these repetitive operations with the help of an agent, you can, in the logical process, Reduce the coupling between the parts. The two Yangchang, the best combination of each other.

Here is a detailed understanding of some of the AOP concepts:

• Aspect (Aspect): A focus on modularity, this focus implementation may be another crosscutting multiple objects. Transaction management is a good example of crosscutting concerns in Java application. Aspect is implemented with spring's advisor or interceptor.

• Connection point (Joinpoint): an explicit point in the execution of a program, such as a call to a method or a specific exception being thrown.

• Notification (Advice): The actions that the AOP framework performs at a particular connection point. Various types of notifications include "around", "before", and "throws" notifications. The notification type is discussed below. Many of the AOP frameworks include spring as a notification model for interceptors, maintaining a chain of interceptors around the connection points.

• Pointcut (Pointcut): Specifies a collection of connection points that the notification will be thrown. An AOP framework must allow developers to specify pointcuts, for example, using regular expressions.

• Introduce (Introduction): Add a method or field to the class being notified. Spring allows the introduction of new interfaces to any object being notified. For example, you can simplify caching by using an introduction that enables any object to implement the IsModified interface.

• Target object: An object that contains a connection point, also known as a notification or proxy object.

AOP Agent (AOP proxy): An AOP framework-created object that contains notifications. In spring, an AOP agent can be either a JDK dynamic proxy or a cglib proxy.

• Weaving (weaving): assembly to create an object to be notified. This can be done at compile time (for example, using the ASPECTJ compiler), or it can be done at run time. Spring, like other pure Java AOP frameworks, completes weaving at run time.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.