SpringBoot?AOP?@Pointcut切入點表達式排除某些類方式
更新時間:2021年11月24日 11:32:46 作者:myfwjy
這篇文章主要介紹了SpringBoot?AOP?@Pointcut切入點表達式排除某些類方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
SpringBoot AOP @Pointcut切入點表達式排除某些類
場景
希望給service包下的所有public方法添加開始和結束的info log,但是需要排除和數據庫相關的service
其他博文都推薦了
@Pointcut("execution(* com.demo.service.*.*(..)) && !execution(* com.demo.service.dbservice.*(..)) ")
類似的用法,但是在實際操作中,發(fā)現&&這個關鍵字無法使用,只能使用and才能編譯通過,并且@Pointcut只識別了前面半句表達式,and(&&)之后的內容被無視了。
使用以下方法滿足了開發(fā)需求
@Pointcut("execution(public * com.demo.service.*.*(..))") public void serviceMethods() { } @Pointcut("execution(public * com.demo.service.dbservice.*(..))") public void serviceMethods2() { } @Pointcut("serviceMethods() && !serviceMethods2()") public void serviceMethods3() { } @Before("serviceMethods3()") public void startLog(JoinPoint joinPoint) { String className = joinPoint.getSignature().getDeclaringType().getSimpleName(); String methodName = joinPoint.getSignature().getName(); logger.info("{}.{} start", className, methodName); }
AOP排除某些類型不攔截
/** * 日志記錄切面 */ @Aspect public class Logger implements ILogger { @Resource(name="logService") private LogService logService ; @Pointcut("execution(* *..*Action*.*(..)) && !execution(* com.audaque.tjfxpt.web.sjcx.LogAction.*(..))") public void actionPointCut() { }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
springboot 同時啟用http/https的配置方法
本文給大家分享springboot 同時啟用http/https的配置方法,通過修改配置文件、增加java配置的方法來實現此操作,具體內容詳情跟隨小編通過本文學習下吧2021-05-05