Spring Aspectj @Before 所有休息方法 [英] Spring Aspectj @Before all rest method

查看:29
本文介绍了Spring Aspectj @Before 所有休息方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在spring引入@GetMapping之前,我们只关心一个注解@RequestMapping,所以,这方面有效

Before spring introduce @GetMapping, there only one annotation we care about @RequestMapping, so, this aspect works

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

但是在我们可以使用@GetMapping@PostMapping之后,这点就不行了,但是这些注解有一个元注解@RequestMapping.

But after we can use @GetMapping, @PostMapping, this point not works, but these annotations have a meta annotation @RequestMapping.

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

Is there any way to easily intercept all @RequestMapping/@{Get,Post,Put,Patch,..}Mapping ?

推荐答案

我发现这个语法这里对我有用!

I found this syntax here works for me!

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

我也可以全部列出

@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.PatchMapping)" +
    "|| @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) {}

这篇关于Spring Aspectj @Before 所有休息方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆