Analysis and configuration of pointcut expression expression in Spring AOP

Posted by Hotkey on Mon, 03 Jan 2022 13:22:39 +0100

Pointcut refers to those methods that need to be executed "AOP", which is described by "Pointcut Expression"
Pointcut can be defined in the following ways or through & & | and! Combine in different ways

Common methods of expression

  • Method parameter matching

        args()

        @args()

  • Method description matching

        execution()

  • The current AOP proxy object type does not match

        this()

  • Target class matching

        target()

        @target()

        within()

        @within()

  • The methods marked with this annotation match

        @annotation()

Among them, execution is the most used, and its format is:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
returning type pattern,name pattern, and parameters pattern are required
RET type pattern: can be * to indicate any return value, class name of full path, etc
Name pattern: Specifies the method name, * represents all
Set * represents all methods starting with set
parameters pattern: specify method parameters (declared type), (..) Represents all parameters, (*) represents one parameter
(*, String) means that the first parameter is any value and the second is of String type

For example:

Execution of any public method:
execution(public * *(..))

Execution of any method starting with "set":
execution(* set*(..))

Execution of any method of AccountService interface:
execution(* com.xyz.service.AccountService.*(..))

Execution of any method defined in the service package:
execution(* com.xyz.service.*.*(..))

Execution of any method of any class defined in the service package and all sub packages:
execution(* com.xyz.service..*.*(..))

Execution of any method of JoinPointObjP2 class defined in pointcutexp package and all sub packages:
execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")
***>Closest (..) Is the method name, close to* (..)) Is the class name or interface name, such as JoinPointObjP2* (..))

Any class in the pointcutexp package
within(com.test.spring.aop.pointcutexp.*)

Any class in the pointcutexp package and all sub packages
within(com.test.spring.aop.pointcutexp..*)

Implements all classes of MyInterface interface. If MyInterface is not an interface, limit a single class of MyInterface
this(com.test.spring.aop.pointcutexp.MyInterface)
***>When a class that implements an interface is AOP, the getBean method must cast as the interface type, not the type of this class

Any method of all classes annotated with @ MyTypeAnnotation
@within(com.elong.annotation.MyTypeAnnotation)
@target(com.elong.annotation.MyTypeAnnotation)

Any method with @ MyTypeAnnotation annotation annotation
@annotation(com.elong.annotation.MyTypeAnnotation)
***>@ within and @ target are annotations for classes, @ annotation is annotations for methods

Method with @ MyMethodAnnotation annotation for parameter
@args(com.elong.annotation.MyMethodAnnotation)
The parameter is a method of String type (run is determined)
args(String)

More references: Detailed explanation of Spring AOP AspectJ pointcut syntax , download address

Configure Pointcut

  • xml form
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "
    default-autowire="byName">
    <bean id="ecoServiceInterceptor"  class="com.elong.eco.EcoServiceInterceptor"/>
    <aop:config>
       <aop:pointcut id="ecoPoint" expression="(execution(public * *..*.*(..))) && (@within(com.elong.eco.annotation.EcoExtension))"/>
       <aop:advisor pointcut-ref="ecoPoint" advice-ref="ecoServiceInterceptor"/>
    </aop:config>
</beans>
  • java annotation:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "
        default-autowire="byName">
        <!-- Automatic scanning bean -->
        <context:component-scan base-package="com.elong.eco"/>
        <!-- open aop Annotation support -->
        <aop:aspectj-autoproxy/>
    </beans>
    
    package com.elong.eco;
    
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    @Compoment
    @Aspect
    public class EcoServiceAspect {
    
    
        @Pointcut("(execution(public * *..*.*(..))) && (@within(com.elong.eco.annotation.EcoExtension))")
        public void ecoAnotationPoint() {
        }
    
        @Around("ecoAnotationPoint()")
        public Object around(ProceedingJoinPoint point) throws Throwable {
            // TODO something
            return point.proceed(); // Do not call point Processed () does not execute the target method
        }
    }

Topics: Spring Boot