码迷,mamicode.com
首页 > 编程语言 > 详细

java注解类型的aop

时间:2019-03-27 10:55:16      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:dna   factor   ntc   ref   UNC   string   aspectj   error   int   

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 + "\t" + functionName + "\t" + resultDTO.getMessage());
        }

    }
}

  

java注解类型的aop

标签:dna   factor   ntc   ref   UNC   string   aspectj   error   int   

原文地址:https://www.cnblogs.com/cc-java/p/10605298.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!