如何防止我的Spring日志记录方面被触发?

时间:2019-06-12 13:15:59

标签: spring aspectj spring-aop

我的Spring Boot代码具有日志记录方面,该方面记录完成方法所需的时间。有些类我不希望这种行为发生,因此我创建了一个NoLogging批注以放置在不想为其运行方面的类上。但这根本不起作用。我仍然看到该方面在带有注释的类上运行。我对Spring AOP的了解还不足以确切地知道在!@annotation(org.apa.idem.sso.aspect.logging.NoLogging)定义的位置。有人可以帮忙吗?

这是方面的实现:

@Component
@Aspect
public class IdemSsoLoggingAspect {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 
     * This method will calculate the Time taken by the public methods
     * 
     * 
     * @param joinPoint
     * @return
     * @throws Throwable
     */

    private final static String executionString = "!@annotation(org.apa.idem.sso.aspect.logging.NoLogging) && execution(public * org.apa.idem.sso.rest.*.*(..)) || execution(public * org.apa.idem.sso.web.controller.*.*(..)) || execution(public * org.apa.idem.sso.service.*.*(..)) ||   @annotation(org.apa.idem.erights.Transactional)";

    @Around(executionString)
    public Object logTimeMethod(ProceedingJoinPoint joinPoint) throws Throwable {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            Object retVal = joinPoint.proceed();
            stopWatch.stop();
            StringBuffer logMessage = new StringBuffer();
            logMessage.append(" execution time: ");
            logMessage.append(stopWatch.getTotalTimeMillis());
            logMessage.append(" ms");
            logMessage.append(joinPoint.getTarget().getClass().getName());
            logMessage.append(".");
            logMessage.append(joinPoint.getSignature().getName());
            logMessage.append("(");
            // append args
            Object[] args = joinPoint.getArgs();
            for (int i = 0; i < args.length; i++) {
                logMessage.append(args[i]).append(",");
            }
            if (args.length > 0) {
                logMessage.deleteCharAt(logMessage.length() - 1);
            }
            logMessage.append(")");
            logger.error("****************** Time taken  " + logMessage.toString());
            return retVal;
    }
}

有人可以告诉我我在做什么错吗?

1 个答案:

答案 0 :(得分:1)

您必须将条件的第二部分放在括号中,因为您具有将为真的OR运算符。

所以第一部分是“非注释”,第二部分是除括号外的所有内容。

!@annotation(org.apa.idem.sso.aspect.logging.NoLogging) 
    && (execution(public * org.apa.idem.sso.rest.*.*(..)) 
       || execution(public * org.apa.idem.sso.web.controller.*.*(..)) 
       || execution(public * org.apa.idem.sso.service.*.*(..)) 
       || @annotation(org.apa.idem.erights.Transactional))

还有切入点:

 @Pointcut("(within(@org.springframework.stereotype.Repository *)" +
    " || within(@org.springframework.stereotype.Service *)" +
    " || within(@org.springframework.web.bind.annotation.RestController *))"
    + " && !@annotation(org.apa.idem.sso.aspect.logging.NoLogging)")