设为首页 加入收藏

TOP

Spring AOP自定义注解方式实现日志管理(四)
2015-11-12 23:05:54 来源: 作者: 【 】 浏览:36
Tags:Spring AOP 定义 注解 方式 实现 日志 管理
sage());
? ? ? ? ? ? ? }
? ? ? ? ? }
? ? ? }
? ?
? ? /**
? ? * 后置通知 用于拦截Controller层记录用户的操作
? ? *
? ? * @param joinPoint 切点
? ? */?
? ? @After("controllerAspect()")?
? ? public? void after(JoinPoint joinPoint) {?
?
? ? ? /* HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();?
? ? ? ? HttpSession session = request.getSession();? */
? ? ? ? //读取session中的用户?
? ? ? // User user = (User) session.getAttribute("user");?
? ? ? ? //请求的IP?
? ? ? ? //String ip = request.getRemoteAddr();
? ? ? ? User user = new User();
? ? ? ? user.setId(1);
? ? ? ? user.setName("张三");
? ? ? ? String ip = "127.0.0.1";
? ? ? ? try {?
? ? ? ? ? ?
? ? ? ? ? ? String targetName = joinPoint.getTarget().getClass().getName();?
? ? ? ? ? ? String methodName = joinPoint.getSignature().getName();?
? ? ? ? ? ? Object[] arguments = joinPoint.getArgs();?
? ? ? ? ? ? Class targetClass = Class.forName(targetName);?
? ? ? ? ? ? Method[] methods = targetClass.getMethods();
? ? ? ? ? ? String operationType = "";
? ? ? ? ? ? String operationName = "";
? ? ? ? ? ? for (Method method : methods) {?
? ? ? ? ? ? ? ? if (method.getName().equals(methodName)) {?
? ? ? ? ? ? ? ? ? ? Class[] clazzs = method.getParameterTypes();?
? ? ? ? ? ? ? ? ? ? if (clazzs.length == arguments.length) {?
? ? ? ? ? ? ? ? ? ? ? ? operationType = method.getAnnotation(Log.class).operationType();
? ? ? ? ? ? ? ? ? ? ? ? operationName = method.getAnnotation(Log.class).operationName();
? ? ? ? ? ? ? ? ? ? ? ? break;?
? ? ? ? ? ? ? ? ? ? }?
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? }
? ? ? ? ? ? //*========控制台输出=========*//?
? ? ? ? ? ? System.out.println("=====controller后置通知开始=====");?
? ? ? ? ? ? System.out.println("请求方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()")+"."+operationType);?
? ? ? ? ? ? System.out.println("方法描述:" + operationName);?
? ? ? ? ? ? System.out.println("请求人:" + user.getName());?
? ? ? ? ? ? System.out.println("请求IP:" + ip);?
? ? ? ? ? ? //*========数据库日志=========*//?
? ? ? ? ? ? SystemLog log = new SystemLog();?
? ? ? ? ? ? log.setId(UUID.randomUUID().toString());
? ? ? ? ? ? log.setDescription(operationName);?
? ? ? ? ? ? log.setMethod((joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()")+"."+operationType);?
? ? ? ? ? ? log.setLogType((long)0);?
? ? ? ? ? ? log.setRequestIp(ip);?
? ? ? ? ? ? log.setExceptioncode( null);?
? ? ? ? ? ? log.setExceptionDetail( null);?
? ? ? ? ? ? log.setParams( null);?
? ? ? ? ? ? log.setCreateBy(user.getName());?
? ? ? ? ? ? log.setCreateDate(new Date());?
? ? ? ? ? ? //保存数据库?
? ? ? ? ? ? systemLogService.insert(log);?
? ? ? ? ? ? System.out.println("=====controller后置通知结束=====");?
? ? ? ? }? catch (Exception e) {?
? ? ? ? ? ? //记录本地异常日志?
? ? ? ? ? ? logger.error("==后置通知异常==");?
? ? ? ? ? ? logger.error("异常信息:{}", e.getMessage());?
? ? ? ? }?
? ? }
? ?
? ? //配置后置返回通知,使用在方法aspect()上注册的切入点
? ? ? @AfterReturning("controllerAspect()")
? ? ? public void afterReturn(JoinPoint joinPoint){
? ? ? ? ? System.out.println("=====执行controller后置返回通知=====");?
? ? ? ? ? ? ? if(logger.isInfoEnabled()){
? ? ? ? ? ? ? ? ? logger.info("afterReturn " + joinPoint);
? ? ? ? ? ? ? }
? ? ? }
? ?
? ? /**
? ? * 异常通知 用于拦截记录异常日志
? ? *
? ? * @param joinPoint
? ? * @param e
? ? */?
? ? @AfterThrowing(pointcut = "controllerAspect()", throwing="e")?
? ? public? void doAfterThrowing(JoinPoint joinPoint, Throwable e) {?
? ? ? ? /*HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();?
? ? ? ? HttpSession session = request.getSession();?
? ? ? ? //读取s
首页 上一页 1 2 3 4 5 下一页 尾页 4/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Spring AOP的注解实例 下一篇Spring使用Cache

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: