一.操作流程
二.主要代码
package com.gaipian.controller;
import com.gaipian.domaim.SysLog;
import com.gaipian.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.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Date;
@Component
@Aspect
public class LogAop {
@Autowired
HttpServletRequest request;//此示例在web。xml中配置了创建监听
@Resource(name = "sysLogService")
private ISysLogService sysLogService;
private Date visitTime;//1
private String visitTimeStr;
private String username;//1
private String ip;//1
private String url;//1
private Long executionTime; //1
private String method; //1
private Class clazz;
private Method m_method; //可以通过该属性获取method方法名
@Before("execution(* com.gaipian.controller.*.*(..))")
public void doBefore(JoinPoint jop) throws NoSuchMethodException {
visitTime = new Date();
clazz = jop.getTarget().getClass();//获取当前类
String methodName = jop.getSignature().getName();//获取方法名
Object[] args = jop.getArgs();//获取参数名称
if (args == null||args.length==0) {
m_method = clazz.getMethod(methodName);//获取方法
}else {
Class[] classArgs = new Class[args.length];
for (int i = 0; i < args.length; i++) {
classArgs[i] = args[i].getClass();//注意此处不能够有基本类型,因此所有的查询发方法中不能够有基本类型数据
}
m_method = clazz.getMethod(methodName, classArgs);
}
}
@After("execution(* com.gaipian.controller.*.*(..))")
public void doAfter(JoinPoint jop) throws Exception {
String url = "";
if (clazz!=null&&m_method!=null&&clazz !=LogAop.class){
//获取类上的注解
RequestMapping classMapping = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
if (classMapping != null) {
String[] classValue = classMapping.value();//RequetMapping中的value字段就是我们所需要的值
url +=classValue[0];
//获取方法上的注解
RequestMapping methodMapping = m_method.getAnnotation(RequestMapping.class);
if (methodMapping!=null){
String[] methodValues = methodMapping.value();
url +=methodValues[0];
//获取ip字段
ip= request.getRemoteAddr();
//获取当前所操作的用户名称,通过SecurityContext获取
SecurityContext context = SecurityContextHolder.getContext();
User user = (User) context.getAuthentication().getPrincipal();
username = user.getUsername();
}
}
}
executionTime = new Date().getTime()-visitTime.getTime();
SysLog sysLog = new SysLog();
sysLog.setExecutionTime(executionTime);
sysLog.setIp(ip);
sysLog.setMethod("类名"+clazz.getName()+"方法名"+m_method.getName());
sysLog.setUrl(url);
sysLog.setUsername(username);
sysLog.setVisitTime(visitTime);
sysLogService.saveLog(sysLog);
}
}
三.遇见问题
- 1.AOP中获取方法使用的args[i].getClass();,因此所有切入点不可以使用基本类型的参数,都需要换成引入类型,如Integer这种。