Another season of gold, three and silver four, summary of Spring's AOP knowledge points

insert image description here
In the Spring framework, AOP (Aspect-Oriented Programming) is an important component that provides an effective way to solve cross-cutting concerns (cross-cutting concerns), such as logging, security, transaction processing, etc. . The realization of AOP depends on three key concepts: pointcut (pointcut), advice (advice) and pointcut expression (pointcut expression).

  1. Pointcut (pointcut)
    Pointcut is one of the most basic concepts in AOP, which specifies where and when to perform advice. In layman's terms, a pointcut is a set of conditions that match certain positions in the program. When the program runs to these locations, the notifications associated with them will be executed.

In the Spring Framework, pointcuts can be declared in the following ways:

@Pointcut("execution(* com.example.service.*.*(..))")
private void serviceMethod() {
    
    }

In the above code, the @Pointcut annotation is used to declare a pointcut, which specifies any method that matches any class in all com.example.service packages. The * in the pointcut expression means any character, ... means any number of parameters. This pointcut can match
methods like com.example.service.UserService.addUser(String name).

In addition to execution expressions, Spring also supports other types of pointcuts, such as within (matching all methods in the specified package), args (matching methods of the specified parameter type), etc.

  1. Advice Advice
    is a block of code that is executed at the point where the pointcut matches. Advice can be executed before, after, or when an exception occurs in the target method. In the Spring Framework, advice can be declared in the following ways:
@Before("serviceMethod()")
public void doBefore() {
    
    
    // 在目标方法执行之前执行的代码
}

In the above code, the @Before annotation is used to declare a pre-advice (before advice), which will be executed at the location where the serviceMethod() pointcut matches. In this example, we execute some code to record some parameter information of the request.

The notification types supported by Spring include:

  • @Before : Executed before the target method is executed
  • @After : Executed after the target method is executed, regardless of whether an exception occurs
  • @AfterReturning : Executed when the target method executes normally and returns the result
  • @AfterThrowing : Executed when the target method throws an exception
  • @Around : You can fully control the execution process of the target method, including whether to execute the target method and when to execute the target method.
  1. Pointcut expressions (pointcut)
    Pointcut expressions are used to specify matching rules for pointcuts. In Spring AOP, a pointcut expression is a string that describes the rules for the pointcut to match. Pointcut expressions contain multiple keywords and symbols, such as wildcards (*), logical operators (&&, ||, !), parameter matchers (args()), method matchers (execution()), types matchers (within()), etc.

Here is an example using a pointcut expression:

@Pointcut("execution(* com.example.service.*.*(..)) && args(name,..)")
private void serviceMethod(String name) {
    
    }

In the above code, we used two keywords: execution and args. execution is used to match methods under the specified package, and the * wildcard means to match any method name. args is used to match parameters of the specified type, here we use the name parameter to limit the matching.

knowledge points

In addition to the above three key concepts, there are some knowledge points related to Spring AOP:

  1. AOP Proxy

The Spring Framework uses dynamic proxies to implement AOP, which can proxy target objects and add notifications before and after method execution. Spring supports two proxy methods: JDK dynamic proxy and CGLIB dynamic proxy. JDK dynamic proxy requires that the target object must implement an interface, while CGLIB dynamic proxy can proxy any class.

  1. AOP aspect

Aspect is an important concept of AOP, which is a combination of advice and pointcut. An aspect can contain multiple advices and multiple pointcuts to describe a set of concerns.

  1. AOP sequence

Advice in Spring AOP can specify the order of execution through the @Order annotation. If no order is specified, the default order of notification types will be executed.

  1. AOP exception handling

When the target method throws an exception, Spring AOP can catch the exception and execute the specified notification through the @AfterThrowing annotation.

  1. AOP Expression Language (EL)

Spring AOP supports the use of Expression Language (EL) to define pointcut expressions. EL language is a simple expression language used to dynamically calculate the value of expressions at runtime. Using EL language can define pointcut expressions more conveniently, making the code easier to maintain.

Guess you like

Origin blog.csdn.net/KRYST4L123/article/details/129928178