一步小心就掉进坑里面了:SpringBoot2 引入 Aop 不生效
SpringBoot2.1.3版本
首先,引入依赖
<!--面向切面--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
配置切面
package com.ddkt365.poster.aspect; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; /** * 切面类记录请求响应日志 * * @author szliugx@gmail.com * @create 2019-05-15 上午00:02 **/ @Aspect @Component @Slf4j public class ApiLogAspect { @Pointcut("execution(public * com.ddkt365.poster.web.controllers..*.*(..))") public void log() { log.info("before cut1, do something"); } @Before("log()") public void doBefore(JoinPoint joinPoint) { log.info("before cut2, do something"); } @After("log()") public void doAfter() { log.info("before cut3, do something"); } @AfterReturning(returning = "object", pointcut = "log()") public void doAfterReturning(Object object) { log.info("before cut4, do something"); this.printOptLog(); } /** * 打印日志方法 */ private void printOptLog() { log.info("before cut5, do something"); } }
就是这么简单的配置,居然翻车了
因为生效的方法必须为 public 申明,刚好测试的控制器(com.ddkt365.poster.web.controllers包下)里的方法省略掉了 public,就造成了配置正确,却怎样也不生效