"The Path of SSH" spring's AOP-layer-by-step--AOP fundamentals (vi)

Source: Internet
Author: User

As we continue to learn from the spring IOC, spring is becoming clearer and our understanding of spring is further deepened. From this blog post we learn the second core of spring: AOP.


What is AOP


AOP (Aspect oriented programming), which is a technique for cutting-plane programming, which implements the unified maintenance of program functions through precompilation and run-time dynamic proxies. Based on the IOC Foundation, AOP is a continuation of OOP (Object oriented programming, OO ). At the same time, AOP is actually the continuation of the GOF design pattern, the design pattern pursues the decoupling between the caller and the callee, improves the flexibility and extensibility of the code, and AOP can be said to be an implementation of this goal.

AOP divides the application system into two parts, the core business logic (concerns) and the general logic of the landscape, the so-called aspect crosscutting enterprise concerns, for example, All medium-to-large applications involve persistent management (persistent), transaction management (Transaction Management), Security Management (safety), log Management (Logging), and debug Management (Debugging).



The difference between AOP and OOP


AOP and oop are literally very similar, but they are two design ideas for different fields. OOP (Object-oriented programming) encapsulates the entity and its genera and behaviors of the business process in an abstract way for clearer and more efficient logical unit partitioning.

AOP is the extraction of facets in the business process, which is faced with a step or stage in the process to obtain a low-coupling between the parts of the logical process . the isolation effect of the compatibility. These two kinds of design thought have the essential difference in the goal.

The above statement may be too theoretical, for a simple example, for the "employee" such a business entity encapsulation, naturally is the task of Oop/ood, we can build an "employee" class and encapsulate the attributes and behaviors associated with the employee. However, it is impossible to encapsulate "employee" with the idea of AOP design.

Similarly, the segmentation of the action fragment for permission checking is the target area of AOP. It is a bit nondescript to encapsulate an action by Ood/oop.

In other words, Ood/oop is for the field of nouns, and AOP is for the verb field.


Basic concepts


To really understand AOP, there are a few important basic concepts to understand first:

1, Aspect (Aspect): to the crosscutting concern of the modular, in fact, is the realization of common functions. such as log plane, permission plane, and so on.

2, connection point (Joinpoint): Is the program in the process of operation can be inserted into the location of the plane. For example, method calls, exception throws, or field modifications, but spring only supports

The connection point at the method level.

3. notification (Advice): An action performed on a particular connection point (joinpoint) of a slice. Notifications are available in various types, where the package

including "Around", "before", and "after" notifications. Many AOP frameworks, including spring, are notification models with interceptors and maintain a point-of-contact-centric interception

The device chain.

4. pointcut (Pointcut): Used to define which connection points (Joinpoint) the notification (Advice) 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.

5. target Object: The object that is about to cut into the plane, that is, those who are notified.

6. Proxy Object: 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 of the target object

Business logic features plus common functionality. The proxy object is transparent to the user, and is the product of the process of running the program.

7. Weaving (Weaving): The process of applying facets to the target object to create a new proxy object. This process can occur at compile time, class loading period and running

, of course, different points of occurrence have different preconditions. In the case of a compile time, for example, a special compiler is required to support this AOP implementation;

Load period, there is a special class loader that supports AOP implementation, and only in the run time, can be directly through the Java language Reflection mechanism and dynamic agent mechanism to dynamically

Realize.


Notification (Advice) type


In order to meet the needs of the reality, the notification type provides 5 kinds, the target method can be all-round processing;

1.before advice: A notification performed before a connection point (Joinpoint), but this notification does not prevent execution before the connection point.

The ApplicationContext uses <aop:before> elements to declare in <aop:aspect>.

2. Afteradvice: Notification executed when a connection point exits (whether it is a normal return or an abnormal exit).

The ApplicationContext uses <aop:after> elements to declare in <aop:aspect>.

3. Afterreturnadvice: A notification that executes after a connection point is completed normally, excluding the case of throwing an exception.

The ApplicationContext uses <aop:after-returning> elements to declare in <aop:aspect>.

4.Around Advice: A notification that surrounds a connection point, similar to the Dofilter method of the filter in the servlet specification in the Web. You can complete the customization before and after the call to the method

Behavior, you can also choose not to execute.

The ApplicationContext uses <aop:around> elements to declare in <aop:aspect>.

5.afterthrowing Advice: Notification that is executed when the method throws an unexpected exit.

The ApplicationContext uses <aop:after-throwing> elements to declare in <aop:aspect>.


The difference between 2 types of AOP agents
AOP supports 2 kinds of proxies, the dynamic proxy of JDK and cglib implementation mechanism. What is the difference between the two:

The JDK is based on an interface implementation: The JDK dynamic agent proxies The class that implements the interface.

Cglib based on inheritance: The Cglib proxy can generate a subclass for the class proxy, primarily for the specified class, because it is inheritance, so it is best not to use the final declaration for the target class.

In general, the use of JDK proxies is encouraged because the business generally abstracts an interface and does not introduce new things. If it is a legacy system that has not previously implemented an interface, then only cglib can be used.


AOP Configuration
Spring AOP is configured in two flavors:

XML style = Implementation of spring AOP using declarative forms

ASPECTJ style = Implement spring AOP with annotations


In the next blog post, we will use two styles to practice Spring AOP, so stay tuned.



"The Path of SSH" spring's AOP-layer-by-step--AOP fundamentals (vi)

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.