Spring Aspectj @所有其余方法之前

wen*_*ner 2 java aop spring aspectj spring-mvc

在春季介绍之前@GetMapping,我们只关心一个注释@RequestMapping,因此,此方面有效

@Before("within(aa.bb.*.rest..*) && execution(public * *(..)) && @within(org.springframework.web.bind.annotation.RestController) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
Run Code Online (Sandbox Code Playgroud)

但是,在我们可以使用@GetMapping之后@PostMapping,这一点不起作用,但是这些注释具有meta注释@RequestMapping。

有什么办法可以轻松地拦截所有@RequestMapping/ @{Get,Post,Put,Patch,..}Mapping?

wen*_*ner 5

我发现这句法这里对我的作品!

@Pointcut("execution(@(@org.springframework.web.bind.annotation.RequestMapping *) * *(..))")
public void requestMappingAnnotations() { }
Run Code Online (Sandbox Code Playgroud)

我也可以全部列出

@Pointcut("within(aa.bb.*.rest..*)  && @within(org.springframework.web.bind.annotation.RestController)")
public void restControllers() {}

@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping) " +
    "|| @annotation(org.springframework.web.bind.annotation.GetMapping)" +
    "|| @annotation(org.springframework.web.bind.annotation.PostMapping)" +
    "|| @annotation(org.springframework.web.bind.annotation.PathVariable)" +
    "|| @annotation(org.springframework.web.bind.annotation.PutMapping)" +
    "|| @annotation(org.springframework.web.bind.annotation.DeleteMapping)"
)
public void mappingAnnotations() {}

@Pointcut("execution(@(@org.springframework.web.bind.annotation.RequestMapping *) * *(..))")
public void requestMappingAnnotations() { }

@Before("restControllers() && requestMappingAnnotations()")
public void onExecute(JoinPoint jp) {}
Run Code Online (Sandbox Code Playgroud)