Spring AOP执行 – 匹配具有特定参数注解和类型的方法

huangapple go评论113阅读模式
英文:

Spring AOP execution - match method with specific parameter annotation and type

问题

给定以下方法:

    public void doSth(@AnnotationA @AnnotationB SomeType param) {
      ...do sth...
    }

和具有以下@Around建议的@Aspect:

    @Around("execution(* *(.., @com.a.b.AnnotationA (*), ..))") 
英文:

Given the following method:

public void doSth(@AnnotationA @AnnotationB SomeType param) {
  ...do sth...
}

and @Aspect with the following @Around advice:

@Around("execution(* *(.., @com.a.b.AnnotationA (*), ..))") 

Is there any possibility to modify the above expression to match a method with a parameter annotated with the @AnnotationA and with the SomeType type and to wildcard anything that would be between those two and before AnnotationA?

Something like (* @com.a.b.AnnotationA * SomeType) so the following methods would be matched:

doSth(@AnnotationA @AnnotationB SomeType param) 
doSth(@AnnotationB @AnnotationA SomeType param) 
doSth(@AnnotationA SomeType param) 

Thanks in advance.

答案1

得分: 1

辅助类:

包 de.scrum_master.app;

public class SomeType {}
包 de.scrum_master.app;

import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;

@Retention(RUNTIME)
public @interface AnnotationA {}
包 de.scrum_master.app;

import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;

@Retention(RUNTIME)
public @interface AnnotationB {}

驱动应用程序:

包 de.scrum_master.app;

public class Application {
  public static void main(String[] args) {
    Application application = new Application();
    SomeType someType = new SomeType();
    application.one(11, someType, "x");
    application.two(someType, new Object());
    application.three(12.34D, someType);
    application.four(22, someType, "y");
    application.five(56.78D, someType);
  }

  // 这些应该匹配
  public void one(int i, @AnnotationA @AnnotationB SomeType param, String s) {}
  public void two(@AnnotationB @AnnotationA SomeType param, Object o) {}
  public void three(double d, @AnnotationA SomeType param) {}

  // 这些不应该匹配
  public void four(int i, @AnnotationB SomeType param, String s) {}
  public void five(double d, SomeType param) {}
}

切面:

包 de.scrum_master.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class MyAspect {
  @Before("execution(* *(.., @de.scrum_master.app.AnnotationA (de.scrum_master.app.SomeType), ..))")
  public void intercept(JoinPoint joinPoint) {
    System.out.println(joinPoint);
  }
}

控制台日志:

execution(void de.scrum_master.app.Application.one(int, SomeType, String))
execution(void de.scrum_master.app.Application.two(SomeType, Object))
execution(void de.scrum_master.app.Application.three(double, SomeType))
英文:

Helper classes:

package de.scrum_master.app;

public class SomeType {}
package de.scrum_master.app;

import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;

@Retention(RUNTIME)
public @interface AnnotationA {}
package de.scrum_master.app;

import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;

@Retention(RUNTIME)
public @interface AnnotationB {}

Driver application:

package de.scrum_master.app;

public class Application {
  public static void main(String[] args) {
    Application application = new Application();
    SomeType someType = new SomeType();
    application.one(11, someType, "x");
    application.two(someType, new Object());
    application.three(12.34D, someType);
    application.four(22, someType, "y");
    application.five(56.78D, someType);
  }

  // These should match
  public void one(int i, @AnnotationA @AnnotationB SomeType param, String s) {}
  public void two(@AnnotationB @AnnotationA SomeType param, Object o) {}
  public void three(double d, @AnnotationA SomeType param) {}

  // These should not match
  public void four(int i, @AnnotationB SomeType param, String s) {}
  public void five(double d, SomeType param) {}
}

Aspect:

package de.scrum_master.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class MyAspect {
  @Before("execution(* *(.., @de.scrum_master.app.AnnotationA (de.scrum_master.app.SomeType), ..))")
  public void intercept(JoinPoint joinPoint) {
    System.out.println(joinPoint);
  }
}

Console log:

execution(void de.scrum_master.app.Application.one(int, SomeType, String))
execution(void de.scrum_master.app.Application.two(SomeType, Object))
execution(void de.scrum_master.app.Application.three(double, SomeType))

huangapple
  • 本文由 发表于 2023年4月7日 04:42:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定