zoukankan      html  css  js  c++  java
  • 使用AOP思想实现日志的添加

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

    <listener>
         <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    --------》书写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);

    }

    }
  • 相关阅读:
    2019/5/13 洛谷P4742 【tarjan缩点 + 拓扑dp】
    图论500题
    欧拉回路与欧拉路径
    二分图的判定
    二分图的最大匹配以及带权匹配【匈牙利算法+KM算法】
    网络流三大算法【邻接矩阵+邻接表】POJ1273
    马拉车算法,mannacher查找最长回文子串
    tarjan算法(强连通分量 + 强连通分量缩点 + 桥(割边) + 割点 + LCA)
    luogu P5774 [JSOI2016]病毒感染 线性 dp
    luguo P2519 [HAOI2011]problem a dp+贪心
  • 原文地址:https://www.cnblogs.com/newcityboy/p/12044032.html
Copyright © 2011-2022 走看看