Facets-oriented programming (AOP) understanding

Source: Internet
Author: User

Traditionally, when writing business logic to process code, we routinely do a few things: logging, transaction control, and permission control, and then writing the core business logic processing code. When the code is finished looking back, I can not help but find that triumphantly Sasa in hundreds of lines of code, really for the core business logic processing only then a few lines, 6-4 shows. Method Complex method, class complex, so the son with helpless regret to spend how many spring and autumn. In this case, if the project comes to an end and suddenly decides to make a big change in authority control, tens of thousands of methods have to be one by one "visiting" and the pain "worse".

If you can extract all the common code from many of the methods in Figure 6-4, place it centrally in a place, and then dynamically weave these common code into the container at run time, at least two questions can be resolved:

Java EE programmers, when writing specific business logic processing methods, only care about the core business logic processing, improve the efficiency, but also make the code changes concise and elegant.

In future maintenance, it is easy to maintain because the business logic code is stored separately from the common code, and the common code is stored centrally.

Aspect-oriented programming AOP technology is to solve this problem is born, the aspect is cross-sectional, 6-5, represents a universal common function, for example, log plane, permission plane and transaction plane.

The following is an example of the implementation of the AOP implementation process (see figure 6-6) of the user-managed Business logic Component (UserService), which provides an in-depth analysis of how the AOP technology is implemented. AOP technology is based on the reflection mechanism and dynamic proxy mechanism of the Java language. During the operation of the business logic component, the AOP container dynamically creates a proxy object for the consumer to invoke, which has successfully cut the slice to the connection point of the target method as the Java EE programmer intended, so that the functionality of the slice and the functionality of the business logic can be executed simultaneously. In principle, the caller calls directly the AOP container dynamically generated proxy object, and then the proxy object calls the target object to complete the original business logic processing, and the proxy object has already synthesized the tangent plane and the business logic method.

Some of the concepts that are covered in figure 6-6 are explained below.

Facets (Aspect): In fact, the realization of common functions. such as log plane, permission plane, transaction plane, and so on. In practice, a common Java class that holds common functionality implementations can be identified as facets by an AOP container, as specified in the configuration.

Notification (Advice): is the specific implementation of facets. The target method is the reference point, depending on where it is placed, there are 5 kinds of pre-notification (before), post-notification (afterreturning), exception Notification (afterthrowing), final notification (after) and surround notification (Around). In practice, it is usually a method in a slice class, which is exactly what kind of notification, which is also specified in the configuration.

Connection point (Joinpoint): The place where the program is able to insert slices during operation. For example, method invocations, 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 often need to be cut into different connection points, and this exact match is defined by the regular expression of the pointcut.

Target object: The object that is about to cut into the plane, that is, the object that is being notified. All of these objects are left with clean core business logic code, and all common functionality code waits for an AOP container to cut in.

Proxy object: 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 equals the core business logic functionality of the target object plus the shared functionality. The proxy object is transparent to the user, and is the product of the process of running the program.

Weaving (Weaving): The process of applying facets to a target object to create a new proxy object. This process can occur in the compilation period, the class loading period and the operating period, of course, different points of occurrence have different preconditions. For example, in the compilation period, a special compiler is required to support this AOP implementation, and in the class loading period, a special class loader supporting AOP implementation is required, which can be implemented dynamically by the reflection mechanism of Java language and dynamic agent mechanism.

Facets-oriented programming (AOP) understanding

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.