一.采用功能最强的环绕通知
@Component@Aspectpublic class LogAop { @Autowired private HttpServletRequest request; @Autowired private ISysLogService sysLogService; /** * 记录日志环绕通知 * @param pjp * @return * @throws Throwable */ @Around("execution(* cn.itcast.controller.*Controller.*(..))") public Object around(ProceedingJoinPoint pjp) throws Throwable { SysLog sysLog = new SysLog(); long startTime = System.currentTimeMillis(); String uri = request.getRequestURI();//获取路径uri String ip = request.getRemoteAddr();//获取ip String className = pjp.getTarget().getClass().getName();//获取类名 String methodName = pjp.getSignature().getName();//获取方法名 String username = SecurityContextHolder.getContext().getAuthentication().getName();//用springsecurity获取用户名 //放行 Object[] args = pjp.getArgs(); Object proceed = pjp.proceed(args); //放行后 long endTime = System.currentTimeMillis(); long executionTime=endTime-startTime; sysLog.setExecutionTime(executionTime); sysLog.setIp(ip); sysLog.setMethod(className+"."+methodName); sysLog.setUrl(uri); sysLog.setVisitTime(new Date()); sysLog.setUsername(username); sysLogService.save(sysLog);//存储日志到数据库中 return proceed; //返回原方法的返回值 }} //环绕通知,注意方法有返回值
2.前置通知与后置通知
//不必在乎内容,理解格式流程就行 @Component@Aspectpublic class LogAop { @Autowired private HttpServletRequest request; @Autowired private ISysLogService sysLogService; private Date visitTime; //开始时间 private Class clazz; //访问的类 private Method method;//访问的方法 //前置通知 主要是获取开始时间,执行的类是哪一个,执行的是哪一个方法 @Before("execution(* com.itheima.ssm.controller.*.*(..))") public void doBefore(JoinPoint jp) throws NoSuchMethodException { 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(); } clazz.getMethod(methodName, classArgs); } } //后置通知 @After("execution(* com.itheima.ssm.controller.*.*(..))") public void doAfter(JoinPoint jp) throws Exception { long time = new Date().getTime() - visitTime.getTime(); //获取访问的时长 String url = ""; //获取url if (clazz != null && method != null && clazz != LogAop.class) { //1.获取类上的@RequestMapping("/orders") RequestMapping classAnnotation = (RequestMapping) clazz.getAnnotation(RequestMapping.class); if (classAnnotation != null) { String[] classValue = classAnnotation.value(); //2.获取方法上的@RequestMapping(xxx) RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class); if (methodAnnotation != null) { String[] methodValue = methodAnnotation.value(); url = classValue[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.setExecutionTime(time); //执行时长 sysLog.setIp(ip); sysLog.setMethod("[类名] " + clazz.getName() + "[方法名] " + method.getName()); sysLog.setUrl(url); sysLog.setUsername(username); sysLog.setVisitTime(visitTime); //调用Service完成操作 sysLogService.save(sysLog); } } } }}