zoukankan      html  css  js  c++  java
  • java注解类型的aop

    import java.lang.reflect.Method;
    import javax.servlet.http.HttpServletRequest;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    @Aspect
    @Component
    public class FileLogAOP {
        private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
        public FileLogAOP() {
        }
    
        @Pointcut("@annotation(com.wilmar.bms.commons.FileLogAnnotation)")
        public void controllerPointcut() {
        }
    
        @AfterReturning(
            pointcut = "controllerPointcut()",
            returning = "rel"
        )
        public void doAround(JoinPoint joinPoint, Object rel) {
            HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
            String operatorName = ((UserDTO)request.getSession().getAttribute("user")).getName();
            ResultDTO resultDTO = (ResultDTO)rel;
            if("操作失败".equals(resultDTO.getMessage())) {
                String methodName = joinPoint.getSignature().getName();
                Method[] methods = joinPoint.getTarget().getClass().getMethods();
                Object[] args = joinPoint.getArgs();
                String functionName = null;
                Method[] arr$ = methods;
                int len$ = methods.length;
    
                for(int i$ = 0; i$ < len$; ++i$) {
                    Method method = arr$[i$];
                    if(method.getName().equals(methodName)) {
                        Class[] parameterTypes = method.getParameterTypes();
                        if(parameterTypes.length == args.length) {
                            FileLogAnnotation fileLog = (FileLogAnnotation)method.getAnnotation(FileLogAnnotation.class);
                            functionName = fileLog.remark();
                            break;
                        }
                    }
                }
    
                this.logger.error("操作人:" + operatorName + "	" + functionName + "	" + resultDTO.getMessage());
            }
    
        }
    }
    

      

  • 相关阅读:
    P3916 图的遍历 题解
    NBL小可爱纪念赛「 第一弹 」 游记(部分题解)
    P4147 玉蟾宫 题解
    十、一些小例子
    九、基础正则表达式BRE
    八.linux系统文件属性知识
    七、linux目录结构知识---实战
    六、linux目录结构知识
    3.20-30岁形成好的习惯
    五、Centos linux系统优化-实战
  • 原文地址:https://www.cnblogs.com/cc-java/p/10605298.html
Copyright © 2011-2022 走看看