SoFunction
Updated on 2025-03-08

Spring AOP point-cut expression usage and description

Point-cut expression

In Spring AOP, a connection point always represents the execution of the method. Point-cutting is a predicate that matches the connection point, and point-cutting expression language describes the point-cutting programmatically.

Point-cut expressions are something we mainly focus on in addition to AOP logic. This summary explains various expressions. There are currently 9 ways to write point-cut expressions in spring aop.

  • execute
  • within
  • this
  • target
  • args
  • @target
  • @within
  • @annotation
  • @args

1. Execute expression

Intercept any public method

execution(public * *(..))

Intercept any method starting with set

execution(* set*(..))

Intercepting methods in classes or interfaces

InterceptAccountService(kind、interface)All methods defined in
execution(* .*(..))

Intercept methods defined in packages, not methods in subpackages

Intercept any method in all classes in the package,**Not included**Classes in subpackages
execution(* .*.*(..))

Methods defined in intercept packages or subpackages

Intercept all methods defined in packages or subpackages
execution(* ..*.*(..))

2. within expression

Expression format: package name.* or package name…*

Intercept any method in the packet, not including the method in the subpackage

InterceptserviceAny method of any class in the package
within(.*)

Methods defined in intercept packages or subpackages

InterceptserviceAny method of any class in a package and subpackage
within(..*)

3. This expression

A proxy object is intercepted for a specified type

Target object usageaopThe generated proxy object must be of the specified type before it will be intercepted,Note that the proxy object generated after the target object is proxyed will only be intercepted if the specified type matches the specified type.
this()

The use of this expression may not be easy to understand. Let me borrow an example to illustrate:

package .demo1;
​
public interface IService {
    void m1();
}

package .demo1;
​
import .slf4j.Slf4j;
import ;
​
@Slf4j
@Component
public class ServiceImpl implements IService {
    @Override
    public void m1() {
        ("Start this test!");
    }
}

package .demo1;
​
import .slf4j.Slf4j;
import ;
import ;
import ;
import ;
import ;
@Aspect
@Component
@Slf4j
public class Interceptor1 {
​
    @Pointcut("this(.)")
    public void pointcut() {
    }
​
    @Around("pointcut()")
    public Object invoke(ProceedingJoinPoint invocation) throws Throwable {
        ("Before method execution");
        Object result = ();
        ("Method execution is completed");
        return result;
    }
}

package .demo1;
​
import .slf4j.Slf4j;
import ;
import ;
import ;
​
@ComponentScan(basePackageClasses = {})
@EnableAspectJAutoProxy
@Slf4j
public class Client {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        IService service = ();
        service.m1();
        ("{}", service instanceof ServiceImpl);
    }
}

Execution results

10:27:12.277 [main] INFO . - Point-cut this test!

10:27:12.277 [main] INFO . - false

  • @EnableAspectJAutoProxy: means that if an object created by spring implements an interface, the default is to use jdk dynamic proxy. If the interface is not implemented, use cglib to create a proxy object. Therefore, the service is an object generated by jdk dynamic proxy. Service instanceof ServiceImpl is false
  • @Pointcut("this(.)") means that the object generated after being proxyed by spring must be . to be intercepted, but the service is not a ServiceImpl type object [this is because the default JDK dynamic proxy is used, so AOP generates a proxy object, so the service is not a ServiceImpl type object], so it will not be intercepted

Modify the code

@EnableAspectJAutoProxy(proxyTargetClass = true)
proxyTargetClass=trueIndicates mandatory usecglibTo generate proxy objects

Execution results:

10:34:50.755 [main] INFO . - Point-cut this test!

10:34:50.756 [main] INFO .demo1.Interceptor1 - The method execution is completed

10:34:50.756 [main] INFO . - true

service is an object of type ServiceImpl, so it will be intercepted, because students who are familiar with CGLIB's theoretical knowledge know that the proxy object generated by CGLIB is a subclass of the source type, so service must be an object of type ServiceImpl, because polymorphism belongs to relationships.

4. Target expression

The target object is intercepted for the specified type

target()
The target object isAccountServiceTypes will be proxyed

The difference between this and target

  • This acts on the proxy object, and target acts on the target object
  • This means that the proxy object generated after the target object is proxyed and the specified type match will be intercepted, and the matching proxy object is
  • target means that the target object and the specified type match will be intercepted, and the target object matches

5. args expression

Match the parameters in the method

There is only one parameter for matching,And the type is
.@Pointcut("args(.)")
- Match multiple parameters
args(type1,type2,typeN)

Match any multiple parameters

Match the first parameter type as.All methods, .. Indicates any parameter
@Pointcut("args(.,..)")

6. @target expression

The matching target object class has a specified annotation

Included in the target object.Annotation1annotation,Any method that calls the target object will be intercepted
@target(.Annotation1)

7. @within expression

Specifies that the match must contain all connection points in a class that has annotation

Statement has.Annotation1All methods in the annotated class will be intercepted
​​​​​​​@within(.Annotation1)

The difference between @target and @within

  • @target(Annotation A): determines whether annotation A is declared in the called target object. If so, it will be intercepted.
  • @within(Annotation A): Determines whether annotation A is declared in the class to which the called method belongs. If so, it will be intercepted
  • @target focuses on the called object, @within focuses on the class where the called method is located

8. @annotation expression

Match the method with specified annotations (annotations are used on the method)

@annotation(.demo2.Annotation1)

The called method contains the specified annotation

9. @args expression

The method parameter belongs to the type specified annotation and is matched

Note: There are specified annotations on the type to which the method parameter belongs, not the method parameter

Match 1 parameter, and the class to which the first parameter belongs has Anno1 annotation

  • @args(.demo1.Anno1)
  • Match multiple parameters, and the types to which multiple parameters belong have specified annotations
  • @args(.demo1.Anno1,.demo1.Anno2)
  • Match multiple parameters, and the first parameter belongs to the Anno1 annotation
  • @args(.demo2.Anno1,…)

Point-cut expression combination

In addition, the three operators of &&, ||, !, can be used to combine point-cut expressions to represent the relationship with or non.

@Pointcut("@target()")
public void repositoryMethods() {}

@Pointcut("execution(* *..create*(Long,..))")
public void firstLongParamMethods() {}

@Pointcut("repositoryMethods() && firstLongParamMethods()")
public void entityCreationMethods() {}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.