Spring Aop之AspectJ注解配置实现日志管理的方法

所属分类: 软件编程 / java 阅读数: 16
收藏 0 赞 0 分享

最近项目要做一个日志功能,我用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)
@Documented
public @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
@Component
public 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注解配置实现日志管理的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Java数据类型的规则

这篇文章主要介绍了Java数据类型的规则的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring整合TimerTask实现定时任务调度

这篇文章主要介绍了Spring整合TimerTask实现定时任务调度的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解SpringMVC使用MultipartFile实现文件的上传

本篇文章主要介绍了SpringMVC使用MultipartFile实现文件的上传,本地的文件上传到资源服务器上,比较好的办法就是通过ftp上传。这里是结合SpringMVC+ftp的形式上传的,有兴趣的可以了解一下。
收藏 0 赞 0 分享

SpringMVC上传文件的三种实现方式

本篇文章主要介绍了SpringMVC上传文件的三种实现方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例

本篇文章主要介绍了微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

浅析Java中的继承与组合

本文将介绍组合和继承的概念及区别,并从多方面分析在写代码时如何进行选择。文中通过示例代码介绍的很详细,有需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

利用反射获取Java类中的静态变量名及变量值的简单实例

下面小编就为大家带来一篇利用反射获取Java类中的静态变量名及变量值的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java启动线程的3种方式对比分析

这篇文章主要为大家对比分析了java启动线程的3种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringMVC上传和解析Excel方法

这篇文章主要介绍了SpringMVC上传和解析Excel方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JAVA中String类与StringBuffer类的区别

这篇文章主要为大家详细介绍了JAVA中String类与StringBuffer类的区别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多