SpringBoot 使用@Aspect進行日誌管理(基於反射代理模式+註解Log)

在上一篇「SpringBoot 使用@Aspect進行日誌管理(基於反射代理模式)」的基礎上,添加註解進行日誌管理
一、添加日誌註解java

import java.lang.annotation.*;

/**
 * 日誌註解
 * Created by 陳梓平 on 2017/9/7.
 */
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {
    /** 要執行的操做類型好比:add **/
    public String operationType() default "";
    /** 要執行的模塊名稱如:Carouse **/
    public String modularTypeName() default "";
}複製代碼

二、修改JournalServiceAspect類git

import com.chen.enums.ResultEnum;
import com.chen.exception.CustomException;
import com.chen.staticInfos.StaticInfo;
import com.chen.utils.JournalUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;

/**
 * 日誌切面
 * Created by 陳梓平 on 2017/9/11.
 */
@Component
@Aspect
public class JournalAspect {
    /**日誌輸出*/
    private static final Logger logger = LoggerFactory.getLogger(JournalAspect.class);

    /**日誌工具類*/
    @Autowired
    private JournalUtils aspectJournalUtils;

    /**service層切面*/
    private final String POINT_CUT = "execution(* com.chen.service..*(..))";

    @Pointcut(POINT_CUT)
    private void pointcut(){}

    /**
     * 後置最終通知(目標方法只要執行完了就會執行後置通知方法)
     * 日誌管理
     * @param joinPoint
     */
    @After(value = "pointcut()")
    @Transactional
    public void doAfterAdvice(JoinPoint joinPoint) throws CustomException, ClassNotFoundException {
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();
        Class targetClass = Class.forName(targetName);
        Method[] methods = targetClass.getMethods();
        int modulerType = -1;
        int opreationType = -1;
        if (methods.length>0){
            for (Method method : methods) {
                if (method.getName().equals(methodName)) {
                    Class[] clazzs = method.getParameterTypes();
                    if (clazzs.length == arguments.length) {
                        if (method.getAnnotation(JournalLog.class)!=null){
                            modulerType = method.getAnnotation(JournalLog.class).modularTypeName();
                            opreationType = method.getAnnotation(JournalLog.class).operationType();
                            break;
                        }
                    }
                }
            }
        }

        //3.添加日誌
        if (modulerType!=-1&&opreationType!=-1)
            //TODO 3.1 從請求獲取用戶id
            aspectJournalUtils.addJournalInfo(modulerType,opreationType, 10086);


    }
}複製代碼

三、修改JournalServiceImpl添加日誌註解spring

/**
 * Created by 陳梓平 on 2017/9/11.
 */
@Service
public class JournalServiceImpl implements JournalService {

    @Override
    @JournalLog(operationType = StaticInfo.OPERATIONTYPE_ADD,modularTypeName = StaticInfo.MODEULARTTYPE_FIRST)
    public Result add() {
        return ResultUtils.success(ResultEnum.OK);
    }
}複製代碼

四、測試結果
1)、接口調用
數據庫

測試結果
測試結果

2)、數據庫添加日誌數據
數據庫測試結果
數據庫測試結果

附件代碼下載:git.oschina.net/CatalpaFlat…
相關文章
相關標籤/搜索