Spring AOP practices and Spring AOP practices
Spring AOP and AspectJ are two different implementations of AOP. The former is a dynamic proxy, and the latter is a static proxy.
The role of AOP is to separate concerns. It separates business-independent logic and encapsulates it into modules to decouple core businesses from general logic.
First, there are several concepts to understand:
- Aspect (aspect): A simple understanding. It splits public logic and encapsulates it into modules. This module is aspect, which can be understood as a function. For example, a transaction is a function.
- Joinpoint: in Spring AOP, the connection point is a method.
- Advice (notification): action taken at a specific connection point
- Pointcut
- Target object: The method actually called.
To facilitate understanding, we think like this: aspect is the function we want to implement, so the thing we need to do with this function is advice, where these functions are applied is pointcut.
In Spring AOP, defining an AOP function takes only three steps:
1. Declare aspect
2. Define joinpoint
3. Define advice
The following example uses two methods to implement an AOP Function
The project structure is as follows:
The dependent jar is as follows:
Method 1: annotation Based on @ AspectJ
Method 2: XML-based configuration file