Simple understanding of AOP (for Aspect-Oriented Programming)

Source: Internet
Author: User

AOP is short for Aspect Oriented Programming, meaning: For Aspect-oriented programming, through pre-compilation and RuntimeDynamic proxyA technology that implements unified maintenance of program functions.

AOP and OOP are two design ideas oriented to different fields.

OOP (Object-Oriented Programming) for business processingEntityAndAttributeAndActionProceedAbstract EncapsulationTo achieve a clearer and more efficient logical unit division.

AOP is applicable to the business processing process.SectionIt is faced withProcedureOrPhaseTo obtain low couplingIsolation effect.

We can understand AOP and OOP from the literal above, but we cannot understand it in the following way:

Oop is actually an encapsulation of the attributes and behaviors of objects, and AOP cannot address this point. However, AOP is used to process a certain step and stage and extract the aspect from it. That is to say, if there are repeated operations in several or more logical processes, AOP can be extracted, and dynamic proxies can be used to achieve unified maintenance of program functions. In this case, it can be too implicit, you may understand the permission judgment and logging. What should we do if we simply use oop? Add permission judgment before each operation? What should I do with logging? Add logs manually at the beginning, end, and exception of each method? All. If AOP is used, repeated operations can be performed through the proxy, and the coupling between parts can be reduced in the logical process. The two are both arrogant and best in combination.

The following describes the concepts of AOP in detail:

  • Aspect (aspect): modularization of a focus, which may cause cross-cutting of multiple objects. Transaction Management is a good example of cross-cutting concerns in J2EE applications. With the spring advisor or interceptor.
  • Joinpoint: a specific point in the execution process, such as a call to a method or a specific exception, is thrown.
  • Advice: The action performed by the AOP framework at a specific connection point. Various types of notifications include "around", "before", and "throws. The notification type is discussed below. Many AOP frameworks, including spring, use the interceptor as the notification model to maintain an interceptor chain around the connection points.
  • Pointcut: Specifies a set of connection points that a notification will be triggered. The AOP framework must allow developers to specify the entry point, for example, using regular expressions.
  • Introduction: adds a method or field to the notified class. Spring allows the introduction of new interfaces to any notified objects. For example, you can use an introduction to implement the ismodified interface for any object to simplify the cache.
  • Target object: The object that contains the connection point, also known as a notification or proxy object.
  • AOP Proxy: the object created by the AOP framework, including notifications. In spring, the AOP proxy can be JDK dynamic proxy or cglib proxy.
  • Weaving: assemble the aspect to create a notification object. This can be completed during compilation (for example, using the aspectj compiler) or at runtime. Like other pure Java AOP frameworks, spring completes weaving at runtime.

SPIn the ring, the AOP proxy is generated and managed by the Spring IoC container, and its dependency is also managed by the IOC container. As for how Spring AOP is implemented in the project, the next blog will take log records as an example.

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.