一步當心就掉進坑裏面了:SpringBoot2 引入 Aop 不生效web
SpringBoot2.1.3版本spring
首先,引入依賴spring-boot
<!--面向切面--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
配置切面post
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,就形成了配置正確,卻怎樣也不生效this