使用AOP思想实现日志的添加

//1、创建日志表syslog-------》创建日志的实体类---------》在web.xml中配置监听


     org.springframework.web.context.request.RequestContextListener

--------》书写LogAop组件,并封装到实体类中,保存到数据库

 

package com.hope.controller;

import com.hope.domain.SysLog;
import com.hope.service.ISysLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
* @author newcityman
* @date 2019/12/14 - 22:44
*/
@Component
@Aspect
public class LogAop {

@Autowired
private HttpServletRequest request;

private Date visitTime; //开始时间
private Class clazz; //访问的类
private Method method; //访问的方法
private Long executionTime; //访问时长

@Autowired
private ISysLogService sysLogService;

//前置通知 主要获取开始时间,执行的是哪个类,哪个方法
@Before("execution(* com.hope.controller.*.*(..))")
public void doBefore(JoinPoint jp) throws Exception {
visitTime = new Date(); //当前时间就是开始的时间
clazz = jp.getTarget().getClass(); //具体访问的类
String methodName = jp.getSignature().getName(); //获取的访问方法的名称
Object[] args = jp.getArgs();

//获取具体执行的方法的method对象
if (args == null || args.length == 0) {
method = clazz.getMethod(methodName);
} else {
Class[] classArgs = new Class[args.length];
for (int i = 0; i < args.length; i++) {
classArgs[i] = args[i].getClass();
}
method = clazz.getMethod(methodName, classArgs);
}


}

@After("execution(* com.hope.controller.*.*(..))")
public void doAfter(JoinPoint jp) throws Exception {
executionTime = new Date().getTime() - visitTime.getTime(); //获取访问时长
String url = "";
//获取url
if (clazz != null && method != null && clazz != LogAop.class) {
//获取类上的@RequestMapping("/***")
RequestMapping clazzAnnotation = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
if (clazzAnnotation != null) {
String[] clazzValue = clazzAnnotation.value();
//获取方法上@RequestMapping("/***")
RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
if (methodAnnotation != null) {
String[] methodValue = methodAnnotation.value();
url = clazzValue[0] + methodValue[0];
}
}
}
//获取访问的ip地址
String ip = request.getRemoteAddr();

//获取系统的当前操作者
SecurityContext context = SecurityContextHolder.getContext();
User user = (User) context.getAuthentication().getPrincipal();
String username = user.getUsername();

//将日志相关信息封装到SysLog对象
SysLog sysLog = new SysLog();
sysLog.setVisitTime(visitTime);
sysLog.setUsername(username);
sysLog.setIp(ip);
sysLog.setUrl(url);
sysLog.setExecutionTime(executionTime);
sysLog.setMethod("[类名] " + clazz.getName() + " [方法名] " + method.getName());

//调用SysLogSservice的方法
sysLogService.save(sysLog);

}

}

你可能感兴趣的:(使用AOP思想实现日志的添加)