如何才能不让aop拦截getter,setter方法?
还是在service层记录日志适合些?

解决方案 »

  1.   

    execution方法里面怎么定义的,楼主把xml中aop配置拿出来晒晒。
      

  2.   

    package com.zjlolife.util;import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;@Aspect
    public class LogInterceptor {
    @Pointcut("execution (* com.zjlolife.action.UserAction.*(..))")
        private void anyMethod() {};
        
        @Around("anyMethod()")
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
         LoggerUtils.setLogger(LogInterceptor.class);
         //Logger logger = LoggerFactory.getLogger(LogInterceptor.class);
         System.out.println("intercepyot");
         Class className = pjp.getTarget().getClass();
    Object[] args = pjp.getArgs();
    String methodName = pjp.getSignature().getName();
    Object result = null;
    try {
    result = pjp.proceed();
    }
    catch(Throwable t) {
    //如果出现异常就记录异常日志
    LoggerUtils.log(className, args, methodName,t);
    throw new Throwable(t);
    }
    //如果操作正常记录日志
    LoggerUtils.log(className, args, methodName);
    return result;
    }
    }
      

  3.   

    做一些逻辑排除,类似于这样
    @Pointcut("!withincode(@org.junit.Test * demo..*(..))")
        public void notInTestMethod() {}
     
        @Pointcut("getStringFieldAnnotatedWithInjectInTheDemoPackage() && notInTestMethod()")
        public void getStringFieldAnnotatedWithInjectInTheDemoPackageAndNotInTestMethod() {}
      

  4.   

    好像没有直接去掉方法的语法,关注一下;可以换个思路,比如给非getter,setter方法加个前缀或是注解,然后再在Pointcut里过滤
      

  5.   

    你可以在doBasicProfiling里判断拦截的方法是不是get或set方法  是就不记录日志  不是则记录日志