2017-03-07 30 views
1

春季之前推出@GetMapping,只存在我們關心一個註解約@RequestMapping,因此,這方面的工作春天的AspectJ @Before所有其他方法

@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

回答

2

我發現這個語法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.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) {}