Spring Aop之AspectJ註解配置實現日誌管理的方法,aopaspectj

來源:互聯網
上載者:User

Spring Aop之AspectJ註解配置實現日誌管理的方法,aopaspectj

最近項目要做一個日誌功能,我用Spring Aop的註解方式來實現。

建立日誌註解

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)@Documentedpublic @interface SysLog { String action() default "";//動作}

建立切面通知類

記錄操作的方法名,參數和花費的時間,使用環繞通知

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@Componentpublic class SysLogAspect { @Autowired private SysLogService sysLogService; /**  * 切入點  */ @Pointcut("@annotation(com.wyj.annotation.SysLog)") public void pointCut() {} /**  * 環繞通知  *   * @param joinPoint  * @return  * @throws Throwable  */ @Around("pointCut()") public Object aroud(ProceedingJoinPoint joinPoint) throws Throwable {  // 開始時間  long beginTime = System.currentTimeMillis();  // 執行目標方法  Object result = joinPoint.proceed();  // 執行時間長度(毫秒)  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());  }  // 擷取目標類名  String className = joinPoint.getTarget().getClass().getName();  // 擷取方法名  String methodName = signature.getName();  sysLogEntity.setMethod(className + "." + methodName + "()");  // 請求的參數  Object[] args = joinPoint.getArgs();  if (args != null && args.length != 0 && args[0] != null) {   sysLogEntity.setParams(args[0].toString());  }  sysLogEntity.setTime(time);  // 儲存系統日誌  sysLogService.save(sysLogEntity); }}

掃描和啟動aop註解

日誌註解的應用

效果

以上這篇Spring Aop之AspectJ註解配置實現日誌管理的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.