Understanding of faceted Programming (AOP)

  • 2020-06-03 05:42:54
  • OfStack

When writing traditional business logic processing code, we usually do a few things habitually: logging, transaction control, permission control, etc., and then write the core business logic processing code. When I look back after the code is written, I can't help but find that only a few lines of code are really used for core business logic processing, as shown in Figure 6-4. Method complex method, class complex class, in this way with helpless regret for many years. That's fine. At the end of a project, when it is suddenly decided that a major change in access control is needed, the pain is compounded by 11 visits to thousands of other methods.


If you could extract all of the common code in the various methods in Figure 6-4, put it somewhere, centrally manage it, and then dynamically weave it into the common code by the container at runtime, you would solve at least two problems:

When programmers write specific business logic processing methods, they only need to care about the core business logic processing, which not only improves work efficiency, but also makes code changes concise and elegant.

Later maintenance is made easier by the fact that the business logic code is stored separately from the common code, and the common code is stored centrally.

The AOP technology was born to solve this problem. A section is a cross section, as shown in Figure 6-5, which represents a common feature that is ubiquitous, such as a log section, a permission section, and a transaction section.

Taking the implementation process of AOP (see Figure 6-6) of user management business logic component UserService as an example, we will make an in-depth analysis of the implementation principle of AOP technology under 1. AOP technology is based on the reflection mechanism and dynamic proxy mechanism of Java language. During the operation of the business logic component, the AOP container dynamically creates a proxy object for the consumer to invoke. The proxy object has successfully cut into the join point of the target method as intended by the Java EE programmer, so that the function of the aspect and the function of the business logic can be executed simultaneously. In principle, what the caller calls directly is actually 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, while the proxy object has synthesized the aspect and the business logic method.

Now some concepts involved in Figure 6-6 are explained as follows.

Section (Aspect) : It is the realization of common functions. Such as log slice, authority slice, transaction slice, etc. In practice, it is usually a common Java class that holds a common functional implementation. The reason why it is recognized as a section by the AOP container is specified in the configuration.

Notification (Advice) : is the concrete implementation of the aspect. Taking the target method as the reference point, it can be divided into five types according to the place of placement: pre-notification (Before), post-notification (AfterReturning), exception notification (AfterThrowing), final notification (After) and circumferential notification (Around). In practical application, it is usually one method in the section class, and the specific type of advice belongs to is also specified in the configuration.

Join point (Joinpoint) : The point at which the program can insert facets during execution. For example, method calls, exception throws, or field modifications, etc., but Spring only supports join points at the method level.

Pointcut (Pointcut) : Used to define which join points advice should cut into. Different advice often needs to cut into different join points, and this exact match is defined by the regular expression of the pointcut.

Target objects (Target) : those that are about to be cut into the aspect, that is, those that are notified. All that is left of these objects is the clean core business logic code, with all the common functional code waiting for the AOP container to come in.

Proxy object (Proxy) : An object that is dynamically created 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. A proxy object is transparent to the consumer and is a product of the program's execution.

Weaving (Weaving) : The process of applying the aspect to the target object to create a new proxy object. This process can take place at compile time, class load time, and run time, with different prerequisites at different points of occurrence. At compile time, for example, a special compiler that supports this AOP implementation is required. During class loading, a special class loader that supports the AOP implementation is required. Only occurs at runtime, which can be implemented dynamically directly through the Java language's reflection mechanism and dynamic proxy mechanism.

The following is the supplement:

AOP is short for Aspect Oriented Programming, which means: aspect-oriented programming, a technique to realize unified maintenance of program functions through precompilation and dynamic agents at runtime.

AOP and OOP are two design ideas for different fields.

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

AOP, on the other hand, extracts the sections in the process of business processing and faces a certain step or stage in the process to obtain the isolation effect of low coupling between each part in the logical process.

We can take AOP and OOP literally and use the following:

OOP is actually on the object's properties and behavior of encapsulation, and AOP for it is impossible, but AOP is processing a certain steps and stages, from the aspect of extracting, that is to say, if several or more logical process, have repeated operation behavior, AOP can be extracted, using the dynamic proxy, realizing the function of application system maintenance, so may be implicit, judging if the permissions, logging, etc., may be understood. What if we just use OOP? Add permission judgments before each operation? What about logging? Add logs manually at the beginning, end, exception of each method? So, if you use AOP to do these repetitive operations with the proxy, you can reduce the coupling between the parts in the logical process. The two should complement each other with strength.

Here are some AOP concepts in more detail:

The & # 8226; Aspect (Aspect) : Modularity of 1 concern that may be implemented by crosscutting additional objects. Transaction management is a good example of crosscutting concerns in J2EE. Aspects are implemented with Advisor or interceptor of Spring.
The & # 8226; Join point (Joinpoint) : A specific point in the execution of a program, such as a method call or a specific exception being thrown.
The & # 8226; Notification (Advice) : Action performed by the AOP framework at a particular join point. Various types of notifications include "around", "before" and "throws" notifications. Notification types are discussed below. Many AOP frameworks, including Spring, are based on the interceptor notification model, maintaining a chain of interceptors "around" the join point.
The & # 8226; Pointcut (Pointcut) : Specifies a collection of 1 series of join points that advice will be raised. The AOP framework must allow developers to specify pointcuts, for example, using regular expressions.
The & # 8226; Introduction (Introduction) : Adds a method or field to the notified class. Spring allows you to introduce a new interface to any object that is notified. For example, you can simplify caching by using an introduction that makes any object implement the IsModified interface.
The & # 8226; Target object (Target Object) : An object that contains a join point, also known as a notified or proxy object.
The & # 8226; AOP Proxy (AOP Proxy) : Object created by the AOP framework, containing notifications. In Spring, the AOP agent can be an JDK dynamic agent or an CGLIB agent.
The & # 8226; Weaving (Weaving) : Assembling aspects to create a notified object. This can be done at compile time (using the AspectJ compiler, for example) or at run time. Sample of Spring and other pure Java AOP frames, woven at run time.
AOP agent in Spring is generated and managed by IoC container of Spring, and its dependencies are also managed by IoC container. As for how Spring's AOP was implemented in the project, the next blog will take a look at logging as an example.


Related articles: