preface
Spring AOP is one of the many great features of Spring, and I’ve been curious about its implementation, so recently, in some spare time, I put together some material on the implementation of Spring AOP. In the process of finishing, and a little in-depth understanding of some excellent framework and tools, such as ASM, CGLIB, AspectJ, feel their knowledge is shallow, of course, it is difficult to restrain the impulse to share, so I decided to do a series of articles – Spring AOP implementation principle.
The paper is divided into five parts:
-
Introduction of AOP
-
Implement a simple AOP
-
Dynamic proxies: JDK Proxy and CGLIB
-
Spring AOP features interpretation
-
Write a Spring AOP framework by hand
The author thinks that the key part is 3 and 5. These two parts have made an in-depth discussion on the realization principle. Students who have some basic knowledge suggest reading the key part directly.
Introduction of AOP
What is the AOP
A Wiki definition of AOP, a programming paradigm designed to isolate cross-domain concerns for modularity. It adds behavioral logic to existing code without modifying it by specifying pointcuts, such as adding logging to functions that begin with set. It enables non-core business logic (such as logging) to be added to the program without messing up the core code. AOP lays the foundation for aspect-oriented software development.
From the definition, we extract several keywords:
- Programming paradigm, similar to object-oriented programming, AOP is a programming paradigm in that it is aspect oriented programming
- Modularization, modularization of programs is the goal of AOP
- Non-core business logic, AOP is applied to non-core businesses such as logging, monitoring, and so on
Let’s use a schematic diagram to illustrate:
As you can see from the figure, there are log services in both business A and business B. The section-oriented programming thought separates these log services from the main business into A separate Aspect, which is then processed in Advice.
What are the concepts in AOP
Let’s use AspectJ’s Join-point model as an example to illustrate the concepts in AOP
-
Join Point: method calls, constructor calls, initializing classes, instantiating objects, reading and writing member variables, exception handling, etc
-
PointCuts: represents a collection of Join points, such as:
execution(* set* (*))Copy the code
Using the method access point as an example, the expression represents matching methods that start with set and have only one argument
-
Advice: code that can be executed before, around, and after an access point
-
Aspect: An Aspect is like an abstract class that not only incorporates the above elements, it can also contain its own attributes, methods, etc. Of course, the Aspect can also be instantiated. In Spring AOP, the application of aspects is simpler, and for ease of understanding, we can think of a aspect as a class that implements a concrete business, such as a class that calculates the execution time of Dao layer methods.
Note: AOP implementation in AspectJ is much more complex than Spring AOP. This article is intended to understand Spring AOP and does not go into depth. Those interested in further study can refer to the official AspectJ documentation attached
Application scenarios for AOP
When we mentioned earlier that AOP primarily applies non-core business logic, let’s look at some common application scenarios for AOP
- monitoring
- The log
- The cache
- authentication
- The transaction
- Exception handling
- persistence
Reference data
Wiki:en.wikipedia.org/wiki/Aspect…
AspectJ:www.eclipse.org/aspectj/doc…
Spring AOP: docs. Spring. IO/Spring/docs…