Java spring AOP Foundation

  • 2021-12-09 08:46:14
  • OfStack

Catalog 1. AOP Overview 2. AOP Related Terms 3. AOP versus Dynamic Agent 3.1 JDK Dynamic Agent 3.2 CGLib Dynamic Agent 3.3

1. Overview of AOP

AOP, that is, aspect-oriented programming, simply speaking, extracts the repeated parts of the code, uses the dynamic agent technology when it needs to be executed, and enhances the method without modifying the source code; AOP is also a key point of Spring framework. Using AOP, each part of business logic can be isolated, which reduces the coupling of each part of business logic, improves the reusability of programs and improves the development efficiency.

2. Related language of AOP

1) Connection point ( Joinpoint ): Methods that need to be intercepted during program execution

2) Entry point ( Pointcut ): Is the rule set by the interception method, a 1-series collection of join points

3) Notice ( advice ): Enhance the implementation code of the connection point (that is, the method that needs to be bound for the connection point)

通知类型 说明
前置通知(Before) 执行连接点方法之前执行
环绕通知(Around) 环绕连接点方法执行
后置通知(After) 执行连接点方法之后执行
后置返回通知(After Running) 在连接点方法返回结果之后执行,如果方法出现异常则不会执行此通知(通常是最后执行)
异常通知(After Throwing) 在连接点方法抛出异常之后执行

4) Target object ( target ): Proxy object, which refers to the notified object, also known as enhanced object

5) Weaving ( Weaving ): The process of inserting facet code into the target object to generate a proxy object

6) Proxy (P roxy ): When a class is enhanced by AOP weaving, a result proxy class is generated

7) Section ( Aspect ): Our interception handling class encapsulates classes for horizontally inserting system functionality such as transactions, logs, and so on.

3. AOP and Dynamic Proxy

The proxy in AOP can be said to be an object dynamically generated by AOP framework, which can be used as a target object. AOP in Spring has two ways to realize dynamic proxy: JDK dynamic proxy and CGLib dynamic proxy.

3.1 JDK Dynamic Agent

The JDK dynamic proxy mainly involves two classes under the java. lang. reflect package: Proxy and InvocationHandler. Among them, InvocationHandler is an interface, which can define crosscutting logic by implementing this interface, and call the code of target class through reflection mechanism, and dynamically devalue crosscutting logic and business logic to 1.

Specific implementation principle:

1) Create your own call processor by implementing InvocationHandlet interface;

2) Create a dynamic proxy by specifying an ClassLoader object and a set of interface for the Proxy class;

3) Obtain the constructor of dynamic proxy class through reflection mechanism, and its only parameter type is the interface type of calling processor;

4) Creating a dynamic proxy class instance through a constructor, and calling a processor object as a parameter during construction;

JDK dynamic proxy is an interface-oriented proxy mode. If the proxy target has no interface, Spring can't do anything. Spring produces a new anonymous implementation class of the proxy interface through the reflection mechanism of Java, which rewrites the enhancement method of AOP.

3.2 CGLib Dynamic Agent

CGLib adopts the underlying bytecode technology, the full name is: Code Generation Libraray, CGLib can create a subclass for a class, and in the subclass, the method interception technology is used to intercept all the calls of parent class methods and weave crosscutting logic conveniently. CGLib is a powerful and high-performance production class library of Code, which can dynamically extend java classes at runtime. Spring inherits the classes to be dynamically proxied by CGlib during runtime, rewrites the methods of parent classes, and realizes AOP aspect-oriented programming.

3.3 Comparison between the two

If the object to be proxy is an implementation class, Spring will use JDK dynamic proxy to open the completion operation (Spring adopts JDK dynamic proxy implementation mechanism by default); Spring forces CGLib to implement dynamic proxy if the object to be proxied is not an implementation class

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: