溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring Aop之AspectJ注解配置如何實(shí)現(xiàn)日志管理

發(fā)布時(shí)間:2021-08-06 11:06:08 來源:億速云 閱讀:141 作者:小新 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)Spring Aop之AspectJ注解配置如何實(shí)現(xiàn)日志管理,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

最近項(xiàng)目要做一個(gè)日志功能,我用Spring Aop的注解方式來實(shí)現(xiàn)。

創(chuàng)建日志注解

package com.wyj.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 日志注解
 * 
 * 
 * @author:WangYuanJun
 * @date:2016年8月26日 下午8:25:35
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SysLog {

 String action() default "";//動(dòng)作

}

創(chuàng)建切面通知類

記錄操作的方法名,參數(shù)和花費(fèi)的時(shí)間,使用環(huán)繞通知

package com.wyj.aspect;

import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wyj.annotation.SysLog;
import com.wyj.entity.SysLogEntity;
import com.wyj.service.SysLogService;

/**
 * 日志切面通知
 * 
 * 
 * @author:WangYuanJun
 * @date:2016年8月26日 下午10:28:57
 */
@Aspect
@Component
public class SysLogAspect {

 @Autowired
 private SysLogService sysLogService;

 /**
  * 切入點(diǎn)
  */
 @Pointcut("@annotation(com.wyj.annotation.SysLog)")
 public void pointCut() {}

 /**
  * 環(huán)繞通知
  * 
  * @param joinPoint
  * @return
  * @throws Throwable
  */
 @Around("pointCut()")
 public Object aroud(ProceedingJoinPoint joinPoint) throws Throwable {

  // 開始時(shí)間
  long beginTime = System.currentTimeMillis();

  // 執(zhí)行目標(biāo)方法
  Object result = joinPoint.proceed();

  // 執(zhí)行時(shí)長(毫秒)
  long time = System.currentTimeMillis() - beginTime;

  // 保存日志
  saveSysLog(joinPoint, time);
  return result;
 }

 /**
  * 保存日志
  * 
  * @param joinPoint
  * @param time
  */
 private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
  MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  Method method = signature.getMethod();
  SysLogEntity sysLogEntity = new SysLogEntity();
  SysLog sysLog = method.getAnnotation(SysLog.class);
  if (sysLog != null) {

   // 注解上的描述
   sysLogEntity.setOperation(sysLog.action());
  }

  // 獲取目標(biāo)類名
  String className = joinPoint.getTarget().getClass().getName();

  // 獲取方法名
  String methodName = signature.getName();
  sysLogEntity.setMethod(className + "." + methodName + "()");

  // 請(qǐng)求的參數(shù)
  Object[] args = joinPoint.getArgs();
  if (args != null && args.length != 0 && args[0] != null) {
   sysLogEntity.setParams(args[0].toString());
  }
  sysLogEntity.setTime(time);

  // 保存系統(tǒng)日志
  sysLogService.save(sysLogEntity);
 }
}

掃描和啟動(dòng)aop注解

Spring Aop之AspectJ注解配置如何實(shí)現(xiàn)日志管理

日志注解的應(yīng)用

Spring Aop之AspectJ注解配置如何實(shí)現(xiàn)日志管理

效果

Spring Aop之AspectJ注解配置如何實(shí)現(xiàn)日志管理

關(guān)于“Spring Aop之AspectJ注解配置如何實(shí)現(xiàn)日志管理”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI