簡體   English   中英

AOP和控制器的春季啟動問題

[英]spring boot problems with aop and controller

當我使用spring boot時,aop不能與注釋控制器一起使用,我該怎么辦?

@Aspect
@Component
@Configuration
public class MethodStatisticsPointcutAspect {

    @Resource
    private CounterService counterService;

    // aop defined
    @Around("@annotation(com.xxx.xxx.metrics.annotation.MethodStatistics)")
    private void around(ProceedingJoinPoint pjp) {
        // do sth
    }
}

我的控制器定義如下:

@RestController
@RequestMapping("/usertest")
public class UserTestController {

    @RequestMapping("/test")
    @MethodStatistics
    String test() {
       // do sth
   }
}

我希望使用所有帶有注解@MethodStatistics的方法的aop管理器,但它根本無法與@controller一起使用〜

spring(和spring-boot)中的運行時aop綁定只能插入公共方法。 如果要插入受保護的,私有的或受包保護的方法,則必須使用AspectJ代碼編織器,而不是spring實現的運行時代理。

我在自己的項目中偶然發現了這個問題,直到閱讀了指出這一點的文檔(我的方法受到保護)

來自文檔:[ http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html ]

由於Spring的AOP框架基於代理的性質,因此從定義上講,無論是JDK代理(不適用)還是CGLIB代理(在技術上可行,但不建議用於AOP的目的),都不會攔截受保護的方法。 結果,任何給定的切入點將僅與公共方法匹配!

如果您的攔截需求包括受保護的/私有方法甚至構造函數,請考慮使用Spring驅動的本機AspectJ編織,而不是Spring的基於代理的AOP框架。 這構成了具有不同特性的AOP使用模式的不同,因此請確保在做出決定之前先熟悉編織。

暫無
暫無

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

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