簡體   English   中英

通過控制器方法上的注釋的Spring AOP不起作用

[英]Spring aop by annotation on controller method does not work

注解

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
    String value();
}

AOP

@Aspect
@Component
public class MyAspect {

@Around("@annotation(MyAnnotation)")
public Object aroundHandler(ProceedingJoinPoint joinPoint) throws Throwable {
    ...
}

調節器

@Controller
public class MyController {

    @RequestMapping(value="/hello", method=RequestMethod.GET)
    @ResponseBody
    @MyAnnotation(value="hello")
    public String hello() {
        return "hello";
    }
}

在上述情況下,我的aop無法正常工作...
它可以與@Controller不會注釋的其他方法配合使用。
它可以與aop表達式和控制器方法配合使用。

是否可以通過控制器方法通過注釋使用aop?

嘗試這個:

@Around("@annotation(myAnnotation)")
public Object aroundHandler(ProceedingJoinPoint joinPoint,MyAnnotation myAnnotation) throws Throwable {
    // Do Something
}

我認為您需要使用@within ...此博客文章可能會對https://www.productiveprogrammer.net/?p=49有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM