利用Spring AOP实现日志管理


Spring AOP请参考这篇文章

通过spring aop的前置通知和后置通知实现
日志记录的有访问时间,持续时间,访问的url
其中url需要用到反射

ip需要添加一个xml来获取request,然后来获取访问者ip

  
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListenerlistener-class>
  listener>

controller 主要代码如下

package controller;

import domain.SysLog;
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.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import service.SysLogService;

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

/**
 * @author Zi10ng
 * @date 2019年7月22日14:56:56
 */
@Component
@Aspect
public class LogAopController {

    @Autowired
    private SysLogService sysLogService;

    @Autowired
    private HttpServletRequest request;
    /**
     * 开始访问时间
     */
    private Date visitTime;
    private Class clazz;
    private Method method;
    /**
     * 前置通知
     * joinPoint 是被代理对象的信息
     * @param jp jp
     */
    @Before("execution(* 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();
            }
            method = clazz.getMethod(methodName,classArgs);

        }
    }

    /**
     * 后置通知
     * @param jp jp
     */
    @After("execution(* controller.*.*(..))")
    public void doAfter(){
        long time = System.currentTimeMillis() - visitTime.getTime();
        String ip = "";
        String url = "";
        if (clazz != null && method != null && clazz != LogAopController.class){

            //获取类的注解
            RequestMapping classAnnotation = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
            if (classAnnotation != null){
                String[] classValue = classAnnotation.value();

                //获取方法的注解
                RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
                if (methodAnnotation != null){
                    String[] methodValue = methodAnnotation.value();
                    url = classValue[0] + methodValue[0];
                }
            }
        }
        ip = request.getRemoteAddr();

        SysLog sysLog = new SysLog();
        sysLog.setExecutionTime(time);
        sysLog.setIp(ip);
        sysLog.setMethod(clazz.getName() + method.getName());
        sysLog.setUrl(url);
        sysLog.setVisitTime(visitTime);

        sysLogService.save(sysLog);
    }
}

你可能感兴趣的:(Java,EE)