Understanding of Aspect-oriented programming (AOP)

Source: Internet
Author: User

When writing traditional business logic processing code, we usually habitually do several things: logging, Transaction Control and Permission control, and then write the core business logic processing code. After writing the code, you can't help but find that only a few lines of code are available for core business logic processing, as shown in 6-4. This is how many spring and autumn have been spent with helplessness and regret. If the project ends, and suddenly decides that a major change is required in permission control, thousands of methods will have to be visited one by one, making it even worse ".

 

If you can extract all the common code from the methods in Figure 6-4 and place it in a specific place for centralized management, and then dynamically weave the common code into the container during the specific operation, at least two problems can be solved:

When writing specific business logic processing methods, Java EE programmers only need to focus on the core business logic processing, which not only improves work efficiency, but also makes code changes concise and elegant.

In future maintenance, because the business logic code is separated from the common code, and the common code is stored in a centralized manner, maintenance becomes simple and easy.

The Aspect-Oriented Programming AOP technology was born to solve this problem. The Aspect-oriented programming is a horizontal aspect, as shown in 6-5, which represents a common function. For example, log, permission, and transaction.

 

The following uses the AOP implementation process of the user management business logic component userservice (see Figure 6-6) as an example to deeply analyze the implementation principles of the AOP technology. The AOP technology is built on the reflection mechanism and dynamic proxy mechanism of the Java language. When the business logic component is running, the AOP container dynamically creates a proxy object for the user to call, the proxy object has successfully cut the aspect into the connection point of the target method according to the intent of the Java EE programmer, so that the aspect function and the business logic function can be executed at the same time. In principle, the caller calls the proxy object dynamically generated by the AOP container, and then the proxy object calls the target object to complete the original business logic processing, the proxy object has merged the aspect and the business logic method.

 

Some concepts involved in Figure 6-6 are explained as follows.

Aspect (aspect): it is actually the implementation of common functions. Such as log aspect, permission aspect, and transaction aspect. In practice, a common Java class is usually used to store common functions. The reason why the Java class can be recognized as a plane by the AOP container is specified in the configuration.

Notification: the specific implementation of the aspect. Taking the target method as a reference point, according to different places, it can be divided into pre-notification (Before), post-notification (afterreturning), exception notification (afterthrowing), and final notification (after) there are 5 types of Surround notifications. In practice, it is usually a method in the face-cutting class. The specific type of notification is also specified in the configuration.

Joinpoint: the point where the program can insert a cut surface during running. For example, method call, exception throw, or field modification, but spring only supports method-level connection points.

Pointcut: used to define the connection points to which the notification should be redirected. Different notifications usually need to be switched to different connection points. This exact match is defined by the regular expression of the starting point.

Target: objects that are about to cut in, that is, objects that are notified. Only the core business logic code is left in these objects, and all the common functional code is waiting for the AOP container to cut in.

Proxy: an object dynamically created after the notification is applied to the target object. The proxy object function is equivalent to the core business logic function of the target object and a common function. Proxy objects are transparent to users and are the product of the program running.

Weaving: The process of applying a plane to a target object to create a new proxy object. This process can occur during the compilation, class loading, and runtime. Of course, different occurrences have different preconditions. For example, a special compiler that supports the implementation of AOP is required when it occurs during the compilation period. When it occurs during the class loading period, a special class loader that supports the implementation of AOP is required; the reflection and dynamic proxy mechanisms of the Java language can be used for dynamic implementation only at runtime.

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.