亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 AspectJ 的日志控制器

使用 AspectJ 的日志控制器

皈依舞 2022-11-02 15:56:53
我有一個 Spring Boot 應用程序,我想記錄一些信息,當調用 Controller 方法 id 時會發生什么。出于某種原因,我的 Aspect 無法正常工作。這是我用@Aspect 注釋的@Component 類:@Pointcut("within(@org.springframework.stereotype.Controller *)")public void controller() {}@Pointcut("execution(* *.*(..))")protected void allMethod() {}@Before("controller()&& allMethod()")public void logBefore(JoinPoint joinPoint) {}當使用 REST 調用任何 Controller 方法時,不會調用 logBefore 方法。
查看完整描述

2 回答

?
慕田峪4524236

TA貢獻1875條經驗 獲得超5個贊

重要提示:正如您所說,您使用的是 Spring Boot 設置,我的假設是您已經實現了 Spring AOP 模塊而不是“實際的”AspectJ 庫。差異是顯著的,因為 AOP 的實現在它們之間有所不同。Spring 使用 AspectJ 注釋來應用代理,而 AspectJ 將代碼“編織”到您的應用程序中。簡而言之,Spring AOP 可能更容易實現,而 AspectJ 提供了更細粒度的功能(例如編譯時編織)??梢栽?a >這里找到比較。


我已經從您在帖子中提供的代碼片段中嘗試了配置。在我添加了幾個注釋后調用了該建議:


@SpringBootApplication

// Be sure to add EnableAspectJAutoProxy and set proxyTargetClass to true

@EnableAspectJAutoProxy(proxyTargetClass = true)

public class DemoApplication {

  ...

}

// Be sure to add @Aspect and @Component

@Component

@Aspect

public class DemoAop {


  private static Logger logger = LoggerFactory.getLogger(DemoAop.class);


  @Pointcut("within(@org.springframework.stereotype.Controller *)")

  public void controller() {

  }


  @Pointcut("execution(* *.*(..))")

  protected void allMethod() {

  }


  @Before("controller()&& allMethod()")

  public void logBefore(JoinPoint joinPoint) {

    logger.info("TEST");

  }


}


查看完整回答
反對 回復 2022-11-02
?
蠱毒傳說

TA貢獻1895條經驗 獲得超3個贊

在運行時,您的控制器使用 @RestController 而不是 @Controller 進行注釋。

只需將切入點更改為 RestController 即可:

 @Pointcut("within(@org.springframework.web.bind.annotation.RestController *)")
 public void controller() {
 }


查看完整回答
反對 回復 2022-11-02
  • 2 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號