国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > Java > 正文

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

2024-07-13 10:17:08
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

最近項(xiàng)目要做一個(gè)日志功能,我用Spring Aop的注解方式來(lái)實(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)@Documentedpublic @interface SysLog { String action() default "";//動(dòng)作}

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

記錄操作的方法名,參數(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@Componentpublic 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 {  // 開(kāi)始時(shí)間  long beginTime = System.currentTimeMillis();  // 執(zhí)行目標(biāo)方法  Object result = joinPoint.proceed();  // 執(zhí)行時(shí)長(zhǎng)(毫秒)  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)類(lèi)名  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,注解配置,日志管理

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

Spring,Aop,AspectJ,注解配置,日志管理

效果

Spring,Aop,AspectJ,注解配置,日志管理

以上這篇Spring Aop之AspectJ注解配置實(shí)現(xiàn)日志管理的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 密山市| 高州市| 赤峰市| 卢湾区| 秭归县| 遂平县| 普洱| 武汉市| 皮山县| 杭锦旗| 泸溪县| 崇信县| 和硕县| 馆陶县| 黄陵县| 凭祥市| 安徽省| 深州市| 民丰县| 开阳县| 田林县| 平潭县| 青浦区| 西林县| 五台县| 鹤山市| 宕昌县| 葫芦岛市| 抚顺市| 龙陵县| 朝阳区| 满城县| 图木舒克市| 咸阳市| 邵东县| 伊金霍洛旗| 洛扎县| 金塔县| 墨竹工卡县| 麻城市| 通江县|