Deep understanding of the basic concepts of spring AOP

Source: Internet
Author: User

deep understanding of the basic concepts of spring AOP

What is AOP exactly?

Spring AOP and IOC are the two most frequently heard words about spring. Now to focus on the term AOP, the IOC puts one side aside, and the following is an excerpt from spring's dominance about facets-oriented:

    • Facet-oriented--spring provides rich support for aspect-oriented programming, allowing for the development of cohesion through the separation of application business logic with system-level services such as auditing (auditing) and transaction (transaction) management. The Application object only implements what they should do-complete the business logic-that's all. They are not responsible (or even conscious) for other system-level concerns, such as log or transaction support.

When it comes to AOP, we have to mention OOP, and we know that OOP is object-oriented programming, and AOP-oriented slicing is actually a complement to object-oriented programming.

OOP: An object as a computational body with its own name, state, and interface to accept external messages. In the object model, a new object is generated, the old object is destroyed, the message is sent, and the response message forms the basis of the OOP computational model. It emphasizes the object's "abstraction", "encapsulation", "Inheritance", and "polymorphism". In other words, OOP allows you to define relationships from top to bottom, but it is not appropriate to define left-to-right relationships. such as logging capabilities. Log code is often spread horizontally across all object hierarchies, and has nothing to do with the core functionality of the objects it spreads to. This is true for other types of code, such as security, exception handling, and transparent persistence. This irrelevant code scattered around is called crosscutting (cross-cutting) code, and in OOP design, it leads to a lot of duplication of code, rather than the reuse of individual modules.

AOP: It uses a kind of called " crosscutting"Technique that splits the encapsulated object inside and encapsulates the public behavior that affects multiple classes into a reusable module, which is named" Aspect ", which is the aspect. The so-called "aspect", simply put, is to encapsulate those logic or responsibilities that are not related to the business, but are called together for business modules, to reduce duplication of code, reduce coupling between modules, and facilitate future operability and maintainability. AOP represents a horizontal relationship, if the "object" is a hollow cylinder, which encapsulates the properties and behavior of the object, then the aspect-oriented programming approach is like a razor that cuts through the hollow cylinders to get inside the message. The cut-off aspect is the so-called "facet". Then it hands the cut-off slices with a clever capture of the heavens, leaving no traces.

As shown in: Log information, permission information, security services exist in three services, using AOP to decouple these three services


Using "crosscutting" techniques, AOP divides software systems into two parts: core concerns and crosscutting concerns. The main process of business process is the core concern, and the part that has little relation is the crosscutting concern. One feature of crosscutting concerns is that they often occur in many of the core concerns and are essentially similar everywhere. Like what permissions authentication, logging, transaction processing。 The role of AOP is to separate the various concerns in the system, separating the core concerns from the crosscutting concerns. As Avanade's senior program architect Adam Magee says, the core idea of AOP is "separating the business logic in the application from the generic services that support it." ”

Some concepts of AOP

Describe some of the terminology commonly used in AOP with notifications (ADIVCE), pointcuts (Pointcut), Connection points (join point), facets (Aspect), ingestion (Introduction), weaving (Weaving)

Notice (Advice)

The notification is divided into V types:

    1. Before called before the method is called
    2. After after the method completes, the notification is invoked, regardless of whether the method succeeds
    3. After-returning call notification After the method executes successfully
    4. After-throwing call notification after method throws an exception
    5. Around notifies you of the method that contains the notification, executes the custom behavior after the call is called before the method is called.
Connection points (join point)

A point during the execution of a, such as the execution of a method or the handling of a exception.

For example: Method invocation, method execution, field set/Get, exception handling execution, class initialization, or even a point in a for loop

In theory, any point in the execution of a program can be used as a loom, and all of these execution points are Joint.

But Spring AOP currently supports only method execution (methods execution)

Tangent point (Pointcut)

Notification (advice) defines when a slice is defined, and a tangent is defined as a slice. where"Describes a class of Joint points, such as defining a lot of Joint point, which is a match for Spring AOP for which methods are executed

Description Method:

    1. Directly specify the method name of the Jointpoint, which is relatively single, and usually supports only the method-level AOP framework

    2. Regular expressions

    3. A specific description language, such as the Pointcut description language provided by AspectJ

Facets (Aspect)

Facets are the combination of tangency points and notifications. The notification and tangency together define the entire contents of the slice, when and where and when and where to complete the function

Introduction (Introduction)

References allow us to add new methods or properties to existing classes

Weave in (Weaving)

Assemble aspects to create a notified object. This can be done at compile time (for example, with the ASPECTJ compiler), or at run time. Spring, like other pure Java AOP frameworks, completes weaving at run time.

Spring's support for AOP

AOP Framework Types

  • Aspectj
  • JBoss AOP
  • Spring AOP

    Spring provides four distinct AOP support

  • Agent-based Classic AOP
  • @AspectJ annotation-driven facets
  • Pure Pojo Facets
  • Injection-type ASPECTJ facets (for each version of spring)

    The previous three are spring agent-based AOP variants, so spring's support for AOP is limited to method interception. If the AOP requirements exceed the simple method interception category, then consider implementing facets in the AspectJ, using spring's di to inject the spring bean into the ASPECTJ slice

    how spring is implemented

    The implementation of AOP technology, mainly divided into two categories: first, the use of dynamic Agent technology, the use of interception of messages to decorate the message to replace the original object behavior, the second is the use of static weaving, the introduction of a specific syntax to create "aspects", so that the compiler can be woven into the relevant "aspects" The code.

    Focus on dynamic Agent technology

    Spring's dynamic agent consists of two parts:

  • JDK Dynamic Agent

      JDK动态代理主要涉及到java.lang.reflect包中的两个类:Proxy和InvocationHandler。InvocationHandler是一个接口,通过实现该接口定义横切逻辑,并通过反射机制调用目标类的代码,动态将横切逻辑和业务逻辑编制在一起。Proxy利用InvocationHandler动态创建一个符合某一接口的实例,生成目标类的代理对象。

  • Cglib Dynamic Agent

        CGLib全称为Code Generation Library,是一个强大的高性能,高质量的代码生成类库,可以在运行期扩展Java类与实现Java接口,CGLib封装了asm,可以再运行期动态生成新的class。和JDK动态代理相比较:JDK创建代理有一个限制,就是只能为接口创建代理实例,而对于没有通过接口定义业务方法的类,则可以通过CGLib创建动态代理。

    The Spring AOP Framework handles the AOP proxy classes in the following principles:

      1. If the implementation class of the target object implements the interface, Spring AOP will use the JDK dynamic Proxy to generate the AOP proxy class;
      2. If the implementation class of the target object does not implement an interface, Spring AOP will use CGLIB to generate an AOP proxy class-but the selection process is completely transparent to the developer and does not need to be cared for by the developer.

    Spring AOP dynamically chooses to use the JDK dynamic proxy, CGLIB to generate an AOP proxy, and if the target class implements an interface, spring AOP does not require CGLIB support, directly using the Proxy and Invocationhandler provided by the JDK into an AOP proxy.
  • Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

    Deep understanding of the basic concepts of spring AOP

    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.