The understanding of aspect-oriented programming (AOP) __ Programming

Source: Internet
Author: User
Tags aop reflection

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.


Article from: http://blog.csdn.net/liujiahan629629/article/details/18864211

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.