欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-13 22:07:10
最近项目要做一个日志功能,我用spring aop的注解方式来实现。 创建日志注解 package com.wyj.annotation; import...

最近项目要做一个日志功能,我用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注解配置实现日志管理的方法

日志注解的应用

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

效果

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

以上这篇spring aop之aspectj注解配置实现日志管理的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:

下一篇: