2 回答

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超5個贊
重要提示:正如您所說,您使用的是 Spring Boot 設(shè)置,我的假設(shè)是您已經(jīng)實(shí)現(xiàn)了 Spring AOP 模塊而不是“實(shí)際的”AspectJ 庫。差異是顯著的,因?yàn)?AOP 的實(shí)現(xiàn)在它們之間有所不同。Spring 使用 AspectJ 注釋來應(yīng)用代理,而 AspectJ 將代碼“編織”到您的應(yīng)用程序中。簡而言之,Spring AOP 可能更容易實(shí)現(xiàn),而 AspectJ 提供了更細(xì)粒度的功能(例如編譯時編織)。可以在這里找到比較。
我已經(jīng)從您在帖子中提供的代碼片段中嘗試了配置。在我添加了幾個注釋后調(diào)用了該建議:
@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貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個贊
在運(yùn)行時,您的控制器使用 @RestController 而不是 @Controller 進(jìn)行注釋。
只需將切入點(diǎn)更改為 RestController 即可:
@Pointcut("within(@org.springframework.web.bind.annotation.RestController *)") public void controller() { }
添加回答
舉報(bào)