2 回答

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");
}
}

TA貢獻1895條經驗 獲得超3個贊
在運行時,您的控制器使用 @RestController 而不是 @Controller 進行注釋。
只需將切入點更改為 RestController 即可:
@Pointcut("within(@org.springframework.web.bind.annotation.RestController *)") public void controller() { }
添加回答
舉報