欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2018年01月22日 14:19:51   作者:wangyuanjun008  
下面小編就為大家分享一篇Spring Aop之AspectJ注解配置實現(xiàn)日志管理的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

最近項目要做一個日志功能,我用Spring Aop的注解方式來實現(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 "";//動作

}

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

記錄操作的方法名,參數(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;

 /**
  * 切入點
  */
 @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 {

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

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

  // 執(zhí)行時長(毫秒)
  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 + "()");

  // 請求的參數(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);
 }
}

掃描和啟動aop注解

日志注解的應用

效果

以上這篇Spring Aop之AspectJ注解配置實現(xiàn)日志管理的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • java7 簡化變參方法調(diào)用實例方法

    java7 簡化變參方法調(diào)用實例方法

    在本篇文章里我們給大家整理的是關于java7 簡化變參方法調(diào)用實例方法以及實例代碼,需要的朋友們學習下。
    2019-11-11
  • Java中的cglib原理解析

    Java中的cglib原理解析

    這篇文章主要介紹了Java中的cglib原理解析,由于代理類繼承了被代理類,所以調(diào)用sayHello()方法時會直接調(diào)用代理類的sayHello()方法,而在代理類的方法中,調(diào)用了Callback的邏輯,需要的朋友可以參考下
    2023-10-10
  • springmvc前臺向后臺傳值幾種方式總結(從簡單到復雜)

    springmvc前臺向后臺傳值幾種方式總結(從簡單到復雜)

    今天小編就為大家分享一篇springmvc前臺向后臺傳值幾種方式總結(從簡單到復雜),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 基于mybatis-plus QueryWrapper 排序的坑

    基于mybatis-plus QueryWrapper 排序的坑

    這篇文章主要介紹了mybatis-plus QueryWrapper 排序的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • SpringBoot快速搭建實現(xiàn)三步驟解析

    SpringBoot快速搭建實現(xiàn)三步驟解析

    這篇文章主要介紹了SpringBoot快速搭建實現(xiàn)三步驟解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • 解讀動態(tài)數(shù)據(jù)源dynamic-datasource-spring-boot-starter使用問題

    解讀動態(tài)數(shù)據(jù)源dynamic-datasource-spring-boot-starter使用問題

    這篇文章主要介紹了解讀動態(tài)數(shù)據(jù)源dynamic-datasource-spring-boot-starter使用問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 詳解Spring Cloud Gateway修改請求和響應body的內(nèi)容

    詳解Spring Cloud Gateway修改請求和響應body的內(nèi)容

    這篇文章主要介紹了Spring Cloud Gateway修改請求和響應body的內(nèi)容的相關資料,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • JDK1.8中的ConcurrentHashMap使用及場景分析

    JDK1.8中的ConcurrentHashMap使用及場景分析

    這篇文章主要介紹了JDK1.8中的ConcurrentHashMap使用及場景分析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Spring注解方式防止重復提交原理詳解

    Spring注解方式防止重復提交原理詳解

    這篇文章主要為大家詳細介紹了Spring注解方式防止重復提交原理,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • java string類方法深入解析

    java string類方法深入解析

    以下是對java中的string類方法進行了詳細的分析介紹。需要的朋友可以過來參考下
    2013-08-08

最新評論