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());
            }
    
        }
    }
    

      

  • 相关阅读:
    mysql存储过程
    Mysql中的触发器
    快速开始、环境搭建、修改包名、新建模块、正式部署
    windows下redis下载安装
    Windows10下mysql 8.0.19 安装配置方法图文教程
    IDEA中安装SVN
    常见页面报错
    Python AttributeError: 'Module' object has no attribute 'STARTF_USESHOWINDOW'
    如何编写一篇高质量的技术博文?学习本文的排名靠前大法
    Linux use apktool problem包体变大GLIBC2.14等问题
  • 原文地址:https://www.cnblogs.com/cc-java/p/10605298.html
Copyright © 2011-2022 走看看