【问题标题】:Spring Aspectj @Before all rest methodSpring Aspectj @Before all rest 方法
【发布时间】:2017-03-07 07:34:26
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: java spring spring-mvc aop aspectj


    【解决方案1】:

    我发现这种语法 here 适合我!

    @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) {}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 1970-01-01
      • 2018-06-28
      相关资源
      最近更新 更多