zoukankan      html  css  js  c++  java
  • aop 记录用户操作(一)

    转载: http://www.cnblogs.com/guokai870510826/p/5981015.html

     

    使用标签来设置需要的记录

    实例:@ISystemLog()

    @Controller
    @RequestMapping("test")
    public class TestController {
        @RequestMapping(value = "test.do", method = RequestMethod.GET)
        @ISystemLog(module = "TestController",methods = "test")
        public ModelAndView test(HttpServletRequest request, HttpServletResponse response) throws Exception {
            HttpServletRequest request1 = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            return new ModelAndView("/login");
        }
    }



     

    创建ISystemLog

    package com.helka.cmis.common.utils.log;
    
    import java.lang.annotation.*;
    @Target({ElementType.PARAMETER, ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface ISystemLog {
        String module()  default "";
        String methods()  default "";
    }


    创建LogAopAction

    package com.helka.cmis.common.utils.log;
    
    import com.helka.cmis.common.utils.LogEntity;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.Signature;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    import org.springframework.web.context.request.ServletWebRequest;
    
    import javax.annotation.Resource;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.lang.reflect.Method;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    @Aspect
    public class LogAopAction {
    
    //    @Resource(name="logService")
    //    private LogServiceImpl logservice;
    
        //配置接入点,如果不知道怎么配置,可以百度一下规则
        @Pointcut("execution(* com.kintech.*.controller..*.*(..))")
        private void controllerAspect(){}
    //    @Autowired
    //    private  HttpServletRequest request;
    
        //@Around("execution(* org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(..))")
        @Around("controllerAspect()")
        public Object around(ProceedingJoinPoint pjp) throws Throwable {
            //常见日志实体对象
            LogEntity log = new LogEntity();
            //获取登录用户账户
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    
            ServletWebRequest servletWebRequest=new ServletWebRequest(request);
            HttpServletResponse response=servletWebRequest.getResponse();
    
            String name = (String) request.getSession().getAttribute("USER_ID");
            log.setAccount(name);
            //获取系统时间
            String time = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date());
            log.setCreatetime(time);
    
            String ip = request.getHeader("X-Real-IP");
            log.setIp(ip);
    
            long start = System.currentTimeMillis();
            // 拦截的实体类,就是当前正在执行的controller
            Object target = pjp.getTarget();
            // 拦截的方法名称。当前正在执行的方法
            String methodName = pjp.getSignature().getName();
            // 拦截的方法参数
            Object[] args = pjp.getArgs();
            // 拦截的放参数类型
            Signature sig = pjp.getSignature();
            MethodSignature msig = null;
            if (!(sig instanceof MethodSignature)) {
                throw new IllegalArgumentException("该注解只能用于方法");
            }
            msig = (MethodSignature) sig;
            Class[] parameterTypes = msig.getMethod().getParameterTypes();
    
            Object object = null;
            // 获得被拦截的方法
            Method method = null;
            try {
                method = target.getClass().getMethod(methodName, parameterTypes);
            } catch (NoSuchMethodException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (SecurityException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            if (null != method) {
                // 判断是否包含自定义的注解,说明一下这里的SystemLog就是我自己自定义的注解
                if (method.isAnnotationPresent(ISystemLog.class)) {
                    ISystemLog systemlog = method.getAnnotation(ISystemLog.class);
                    log.setModule(systemlog.module());
                    log.setMethod(systemlog.methods());
                    try {
                        object = pjp.proceed();
                        long end = System.currentTimeMillis();
                        //将计算好的时间保存在实体中
                        log.setUsedtime(end-start);
                        log.setCommit("Success!");
                        //保存进数据库
                        //logservice.saveLog(log);
                    } catch (Throwable e) {
                        // TODO Auto-generated catch block
                        long end = System.currentTimeMillis();
                        log.setUsedtime(end-start);
                        log.setCommit("Failed");
                        //logservice.saveLog(log);
                    }
                } else {//没有包含注解
                    object = pjp.proceed();
                }
            } else { //不需要拦截直接执行
                object = pjp.proceed();
            }
            return object;
        }
    }


    创建数据库的映射实体

    public class LogEntity {
    
        private int id;
        private String account;
        private String module;
        private String method;
        private long usedtime;
        private String ip;
        private String createtime;
        private String commit;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getAccount() {
            return account;
        }
    
        public void setAccount(String account) {
            this.account = account;
        }
    
        public String getModule() {
            return module;
        }
    
        public void setModule(String module) {
            this.module = module;
        }
    
        public String getMethod() {
            return method;
        }
    
        public void setMethod(String method) {
            this.method = method;
        }
    
        public long getUsedtime() {
            return usedtime;
        }
    
        public void setUsedtime(long usedtime) {
            this.usedtime = usedtime;
        }
    
        public String getIp() {
            return ip;
        }
    
        public void setIp(String ip) {
            this.ip = ip;
        }
    
        public String getCreatetime() {
            return createtime;
        }
    
        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }
    
        public String getCommit() {
            return commit;
        }
    
        public void setCommit(String commit) {
            this.commit = commit;
        }
    
    
    }


    配置文件:

    springmvc-servlet.xml

    <aop:aspectj-autoproxy proxy-target-class="true" />
        <bean id="logAopAction" class="com.helka.cmis.common.utils.log.LogAopAction" />



     

     

  • 相关阅读:
    ERROR: epmd error for host "yourhostname": timeout
    leetcode485
    leetcode463
    leetcode496
    leetcode344
    leetcode412
    leetcode500
    leetcode476
    leetcode557
    leetcode461
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/9779778.html
Copyright © 2011-2022 走看看