编写精确的切入点表达式

时间:2015-05-20 06:11:40

标签: spring spring-aop pointcut

我正在使用Spring AOP进行日志记录,其中我想记录包中存在的所有方法的输入/输出。我已经为目标包编写了以下切入点。

@Pointcut("within(com.mypackage.model.*)")
public void allmethods(){};

我的记录方法如下。

@Before("allmethods()")
    public void LoggingAdviceBefore(JoinPoint joinPoint) 
    {
        StringBuffer logMessage = new StringBuffer();
        if(joinPoint != null && joinPoint.getTarget()!=null && joinPoint.getTarget().getClass()!=null)
        {
            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(")");
            log.info(logMessage.toString());
        }

    }

代码工作正常。

我的问题是,即使我做了一些简单的操作,比如在我的代码中填充数组列表,即使该信息也被记录下来。我不希望记录这些信息。

我想仅记录我在目标包&amp;存在的类中编写的方法的输入。不是为了在这些方法中编写的代码。我该如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

您可以使用我在几个月前编写的以下代码来了解SNMP框架实现,它打印包和子包中所有方法的i / o,您可以根据需要删除不相关的类并根据需要进行修改。

@Aspect
public class Snmp4JProfiler {

    private static final Logger LOG = LoggerFactory.getLogger(Snmp4JProfiler.class);

    @Pointcut("execution (* org.snmp4j.Snmp.*(..))")
    public void allSnmpServices() {
    }

    @Pointcut("execution (* org.snmp4j.security.U*.*(..))")
    public void allUSMServices() {
    }

    @Around("allUSMServices() || allSnmpServices()")
    public Object executionTimeOfService(ProceedingJoinPoint pjp) throws Throwable {

        MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
        String className = pjp.getSignature().getDeclaringTypeName();
        final String methodName = methodSignature.getName();


        String methodArgs = "";
        for (Object obj : pjp.getArgs()) {

            if (obj == null) {
                methodArgs += "no args or null,";
            } else if (obj instanceof UsmTimeEntry) {
                UsmTimeEntry timeEntry = (UsmTimeEntry) obj;
                methodArgs += obj.toString() + "[" + timeEntry.getEngineBoots() + "," + timeEntry.getLatestReceivedTime() + ","
                        + timeEntry.getTimeDiff() + "," + timeEntry.getEngineID() + "]";
            } else if (obj instanceof Object[]) {
                methodArgs += obj.toString() + " " + Arrays.toString((Object[]) obj);
            } else {
                methodArgs += obj;
            }

        }

        LOG.info("Start of method#" + methodName + " #class#" + className + " #args#" + methodArgs);

        try {
            Object output = pjp.proceed();

            String rtValues = "";
            if (output == null) {
                rtValues += "no args or null,";
            } else if (output instanceof UsmTimeEntry) {
                UsmTimeEntry timeEntry = (UsmTimeEntry) output;
                rtValues += output.toString() + "[" + timeEntry.getEngineBoots() + "," + timeEntry.getLatestReceivedTime() + ","
                        + timeEntry.getTimeDiff() + "," + timeEntry.getEngineID() + "]";
            } else if (output instanceof Object[]) {
                rtValues += output.toString() + " " + Arrays.toString((Object[]) output);
            } else {
                rtValues += output;
            }

            LOG.info("End of method#" + methodName + " #class#" + className + " #return#" + rtValues);
            return output;
        } catch (Exception ex) {
            LOG.info("End of method#" + methodName + " #class#" + className + " #error#" + ex.getMessage());
            throw ex;
        }

    }

}