SpringBoot AOP @Pointcut切入点表达式排除某些类
场景
希望给service包下的所有public方法添加开始和结束的info log,但是需要排除和数据库相关的service
其他博文都推荐了
- @Pointcut("execution(* com.demo.service.*.*(..)) && !execution(* com.demo.service.dbservice.*(..)) ")
类似的用法,但是在实际操作中,发现&&这个关键字无法使用,只能使用and才能编译通过,并且@Pointcut只识别了前面半句表达式,and(&&)之后的内容被无视了。
使用以下方法满足了开发需求
- @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() {
- }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。