LogAspect.java 1.52 KB
package com.xkl.authorization.aspect;

import lombok.extern.apachecommons.CommonsLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * Created by win7 on 2017/1/14.
 */
@Aspect
@Component
@CommonsLog
public class LogAspect {
    /**
     * 切面,定位到@ExecuteTimeAnnotation注解
     */
    @Pointcut("@annotation(com.xkl.authorization.annotation.LogAnnotation)")
    public void logPointCut() {
    }

    @Before("logPointCut()")
    public void doBefore(JoinPoint joinPoint){
        StringBuilder sb=new StringBuilder();
        Signature signature=joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        Object[] args = joinPoint.getArgs();
        sb.append("\n方法:\n").append(method.getDeclaringClass().getSimpleName())
                .append("-").append(signature.getName()).append("\n").append("参数:\n");
        boolean flag=true;
        if(args!=null){
            for(Object obj:args){
                if(flag){
                    flag=false;
                }else{
                    sb.append(",");
                }
                sb.append(obj.toString());
            }
        }
        log.info(sb.toString());
    }

}