2 回答

TA贡献1875条经验 获得超5个赞
重要提示:正如您所说,您使用的是 Spring Boot 设置,我的假设是您已经实现了 Spring AOP 模块而不是“实际的”AspectJ 库。差异是显着的,因为 AOP 的实现在它们之间有所不同。Spring 使用 AspectJ 注释来应用代理,而 AspectJ 将代码“编织”到您的应用程序中。简而言之,Spring AOP 可能更容易实现,而 AspectJ 提供了更细粒度的功能(例如编译时编织)。可以在这里找到比较。
我已经从您在帖子中提供的代码片段中尝试了配置。在我添加了几个注释后调用了该建议:
@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() { }
添加回答
举报