Home  >  Article  >  Web Front-end  >  Understanding aspect-oriented programming (AOP)_javascript skills

Understanding aspect-oriented programming (AOP)_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:01:301040browse

When writing traditional business logic processing code, we usually habitually do several things: logging, transaction control, permission control, etc., and then write the core business logic processing code. When I finished writing the code and looked back, I couldn't help but find that among the hundreds of lines of code in Yangyangshasha, only a few lines were actually used for core business logic processing, as shown in Figure 6-4. Method after method, class after class, so many years passed with helplessness and regret. That's okay, but if at the end of the project, it is suddenly decided that major changes are needed in the permission control, thousands of methods will have to "visit" one by one, and the pain will be "worse".


If all the common codes in the many methods in Figure 6-4 can be extracted, placed somewhere for centralized management, and then the container can dynamically weave these common codes into each other during specific runtime, at least two problems can be solved. Question:

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

In future maintenance, since the business logic code and the common code are stored separately, and the common code is stored centrally, the maintenance work becomes simple and easy.

Aspect-oriented programming AOP technology was born to solve this problem. Aspect is a cross-section, as shown in Figure 6-5, which represents a ubiquitous common function, such as log aspect, permission aspect and transaction aspect. wait.

Let’s take the AOP implementation process of the user management business logic component UserService (see Figure 6-6) as an example to deeply analyze the implementation principle of AOP technology. AOP technology is based on the reflection mechanism and dynamic proxy mechanism of the Java language. When the business logic component is running, the AOP container will dynamically create a proxy object for the user to call. This proxy object has successfully cut the aspect to the connection point of the target method according to the intention of the Java EE programmer, so that the function of the aspect is consistent with the connection point of the target method. The functions of business logic are executed simultaneously. In principle, what the caller directly calls 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, and the proxy object has synthesized aspects and business logic methods.

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

Aspect: In fact, it is the realization of common functions. Such as log aspects, permission aspects, transaction aspects, etc. In actual applications, it is usually an ordinary Java class that stores the implementation of common functions. The reason why it can be recognized as an aspect by the AOP container is specified in the configuration.

Advice: It is the specific implementation of aspects. Taking the target method as the reference point, according to different placement places, it can be divided into five types: pre-notification (Before), post-notification (AfterReturning), exception notification (AfterThrowing), final notification (After) and surrounding notification (Around). In actual applications, it is usually a method in the aspect class. The specific type of notification is also specified in the configuration.

Joinpoint: It is the place where aspects can be inserted during the running process of the program. For example, method invocation, exception throwing or field modification, etc., but Spring only supports method-level connection points.

Pointcut: Used to define which connection points the notification should cut into. Different notifications usually need to cut into different connection points. This precise matching is defined by the regular expression of the entry point.

Target: It is those objects that are about to enter the aspect, that is, those objects that are notified. Only the clean core business logic code is left in these objects, and all the common function codes are waiting for the AOP container to be cut in.

Proxy object (Proxy): An object that is dynamically created after applying notifications 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 a product of the running process of the program.

Weaving: The process of applying aspects to the target object to create a new proxy object. This process can occur during the compilation period, class loading period and runtime. Of course, different occurrence points have different prerequisites. For example, if it occurs during compilation, it requires a special compiler that supports this AOP implementation; if it occurs during class loading, it requires a special class loader that supports AOP implementation; only if it occurs during runtime, it can be directly passed Java language reflection mechanism and dynamic proxy mechanism to achieve dynamic implementation.

The following is a supplement:

AOP is the abbreviation of Aspect Oriented Programming, which means: Aspect-oriented programming, a technology that achieves unified maintenance of program functions through pre-compilation and runtime dynamic agents.

AOP and OOP are two design ideas for different fields.

OOP (Object-Oriented Programming) abstractly encapsulates the entities and their attributes and behaviors of the business processing process to obtain a clearer and more efficient division of logical units.

AOP extracts aspects in the business processing process. It deals with a certain step or stage in the processing process to obtain the isolation effect of low coupling between various parts in the logical process.

We can understand AOP and OOP simply from the above literal meaning, and it is not an exaggeration to use the following understanding:

OOP is actually an encapsulation of the properties and behaviors of objects, and AOP cannot talk about this. However, AOP deals with certain steps and stages, and extracts aspects from them. In other words, if several or more logical processes, if there are repeated operational behaviors, AOP can extract them and use dynamic agents to achieve unified maintenance of program functions. This may be too implicit. If it comes to permission judgment, logging, etc., it may be Understood. If we simply use OOP, what about permission judgment? Add permission judgment before each operation? What about logging? Manually add logs at the beginning, end, and exceptions of each method? Therefore, if you use AOP to complete these repeated operations with the help of a proxy, you can reduce the coupling between various parts in the logical process. The two complement each other's strengths and complement each other.

Let’s learn more about some AOP concepts:

•Aspect: The modularization of a concern. The implementation of this concern may also cross-cut multiple objects. Transaction management is a good example of a cross-cutting concern in J2EE applications. Aspects are implemented using Spring's Advisor or interceptor.
•Joinpoint: A clear point in program execution, such as a method call or a specific exception being thrown.
•Notification (Advice): At a specific connection point, the action performed by the AOP framework. Various types of notifications include "around", "before" and "throws" notifications. Notification types are discussed below. Many AOP frameworks, including Spring, use interceptors as notification models and maintain an interceptor chain "around" the connection point.
•Pointcut: Specifies a collection of connection points where a notification will be triggered. The AOP framework must allow developers to specify entry points, for example, using regular expressions.
•Introduction: Add methods or fields to the notified class. Spring allows the introduction of new interfaces to any advised object. For example, you can use an import to make any object implement the IsModified interface to simplify caching.
•Target Object: The object containing the connection point, also known as the notified or proxied object.
•AOP Proxy: An object created by the AOP framework, including notifications. In Spring, the AOP proxy can be a JDK dynamic proxy or a CGLIB proxy.
•Weaving: Assembling aspects to create a advised object. This can be done at compile time (e.g. using the AspectJ compiler) or at runtime. Spring, like other pure Java AOP frameworks, completes weaving at runtime.
The AOP proxy in Spring is generated and managed by Spring's IoC container, and its dependencies are also managed by the IoC container. As for how Spring's AOP is implemented in the project, the next blog will use logging as an example to learn.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn