Understanding of Aspect-oriented programming (AOP) and Aspect-oriented programming (aop)

Source: Internet
Author: User

Understanding of Aspect-oriented programming (AOP) and Aspect-oriented programming (aop)

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.

The following is a supplement:

AOP is short for Aspect Oriented Programming, meaning: a technology that implements unified maintenance of program functions through the pre-compilation method and dynamic agent during runtime.

AOP and OOP are two design ideas oriented to different fields.

OOP abstracts and encapsulates the entities, attributes, and behaviors of the business processing process to achieve clearer and more efficient logical unit division.

AOP extracts the aspect of the business processing process. It faces a certain step or stage in the processing process to achieve the isolation effect of low coupling between each part in the logic process.

We can understand AOP and OOP from the literal above, but we cannot understand it in the following way:

OOP is actually an encapsulation of the attributes and behaviors of objects, and AOP cannot address this point. However, AOP is used to process a certain step and stage and extract the aspect from it. That is to say, if there are repeated operations in several or more logical processes, AOP can be extracted, and dynamic proxies can be used to achieve unified maintenance of program functions. In this case, it can be too implicit, you may understand the permission judgment and logging. What should we do if we simply use OOP? Add permission judgment before each operation? What should I do with logging? Add logs manually at the beginning, end, and exception of each method? All. If AOP is used, repeated operations can be performed through the proxy, and the coupling between parts can be reduced in the logical process. The two are both arrogant and best in combination.

The following describes the concepts of AOP in detail:

• Aspect (Aspect): modularization of a focus, which may cause cross-cutting of multiple objects. Transaction Management is a good example of cross-cutting concerns in J2EE applications. With the Spring Advisor or interceptor.
• Joinpoint: a specific point during program execution, such as a call to a method or a specific exception, is thrown.
• Advice: The action performed by the AOP framework at a specific connection point. Various types of notifications include "around", "before", and "throws. The notification type is discussed below. Many AOP frameworks, including Spring, use the interceptor as the notification model to maintain an interceptor chain around the connection points.
• Pointcut: Specifies a set of connection points that a notification will be triggered. The AOP framework must allow developers to specify the entry point, for example, using regular expressions.
• Introduction: adds a method or field to the notified class. Spring allows the introduction of new interfaces to any notified objects. For example, you can use an introduction to implement the IsModified interface for any object to simplify the cache.
• Target Object: The Object that contains the connection point, also known as a notification or proxy Object.
• AOP Proxy: an object created by the AOP framework, including notifications. In Spring, the AOP proxy can be JDK dynamic proxy or CGLIB proxy.
• Weaving: assemble the aspect to create a notification object. This can be completed during compilation (for example, using the AspectJ compiler) or at runtime. Like other pure Java AOP frameworks, Spring completes weaving at runtime.
In Spring, the AOP proxy is generated and managed by the Spring IoC container, and its dependency is also managed by the IoC container. As for how Spring AOP is implemented in the project, the next blog will take log records as an example.

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.