controller日志AOP处理
来源:互联网 发布:js原型链继承面试题 编辑:程序博客网 时间:2024/06/05 07:30
1 定义注解
import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface WebLog { /** * 操作类型 * 如:inbound */ public String operationType() default ""; /** * 操作名称 * 如:入库 * @return */ public String operationName() default ""; /** * 业务类型 * @return */ public String businessType() default "";}
2 AOP切面处理
import java.lang.reflect.Method;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.web.context.request.RequestAttributes;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import com.dzmsoft.framework.base.exception.OAuthException;import com.dzmsoft.framework.base.util.CheckEmptyUtil;import com.dzmsoft.framework.base.util.HttpUtil;import com.dzmsoft.framework.log.dto.LogConstant;import com.dzmsoft.framework.log.service.LogService;import com.dzmsoft.framework.web.pojo.ShiroUser;import com.dzmsoft.framework.web.util.UserUtil;import com.google.gson.Gson;/** * 页面级Controller切面 * @author dzm * */@Aspect@Componentpublic class WebLogAspect { private static final Logger logger = LoggerFactory .getLogger(WebLogAspect.class); @Autowired private LogService logService; @Autowired private Gson gson; @Pointcut("@annotation(com.dzmsoft.framework.log.aop.WebLog)") public void controllerAspect() { } /** * 配置controller环绕通知,使用在方法aspect()上注册的切入点 * @param joinPoint * @throws Throwable */ @Around("controllerAspect()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { long start = System.currentTimeMillis(); Object result = null; try { String targetName = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); logger.info("执行类:{}, 方法:{}开始", targetName, methodName); result = joinPoint.proceed(); long end = System.currentTimeMillis(); if (logger.isInfoEnabled()) { logger.info("执行类:{}, 方法:{},总共耗时{}ms", targetName, methodName, (end - start) ); } logger.info("执行类:{}, 方法:{}结束", targetName, methodName); } catch (Throwable e) { if (logger.isInfoEnabled()) { logger.error("异常结束,结束原因:{}", e.getMessage()); } throw e; } return result; } /** * 后置通知 用于拦截Controller层记录用户的操作 * @param joinPoint */ @SuppressWarnings("rawtypes") @After("controllerAspect()") public void after(JoinPoint joinPoint){ ShiroUser shiroUser = UserUtil.getCurrentShiroUser(); if (shiroUser == null || CheckEmptyUtil.isEmpty(shiroUser.getId())){ return ; } try{ Object[] args = joinPoint.getArgs(); // 方法的参数 HttpServletRequest request = getRequest(args); Map<String, String> params = HttpUtil.getRequestPara(request, false); String targetName = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); Class targetClass = Class.forName(targetName); Method[] methods = targetClass.getMethods(); String operationType = ""; String operationName = ""; String bussinessType = ""; String content = CheckEmptyUtil.isEmpty(params)?null:gson.toJson(params); for (Method method:methods){ if (method.getModifiers()!=1 || !methodName.equals(method.getName())){ // 修饰符1表示public方法 continue; } operationType = method.getAnnotation(WebLog.class).operationType(); operationName = method.getAnnotation(WebLog.class).operationName(); bussinessType = method.getAnnotation(WebLog.class).businessType(); // 添加日志进入hadoop中 logService.addLog2Hadoop(shiroUser.getId(), shiroUser.getLoginName(), shiroUser.getName(),content, operationType, operationName, LogConstant.WEB, bussinessType); // 找到匹配的即终止 break; } } catch(Exception e){ logger.error("==后置通知异常=="); logger.error("异常信息:{}", e.getMessage()); } } private HttpServletRequest getRequest(Object[] args) throws OAuthException { // ServletRequestAttributes may be null, check it first. RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); if (requestAttributes != null) { return ((ServletRequestAttributes) requestAttributes).getRequest(); } logger.debug("未找到HttpServletRequest参数"); throw new IllegalArgumentException("未找到HttpServletRequest参数"); }}
阅读全文
0 0
- controller日志AOP处理
- 日志AOP拦截Controller
- Spring AOP处理日志
- Spring AOP处理日志
- Spring AOP处理日志
- Spring-Aop处理记录日志
- spring-Aop应用日志处理
- controller aop
- Spring Aop自定义注解拦截Controller实现日志管理
- AOP注解日志处理简单理解
- AOP注解日志处理简单理解
- Spring aop切面做日志处理操作
- AOP注解日志处理简单理解
- spring aop 异步处理系统日志
- AOP注解日志处理简单理解
- SpringBoot AOP统一处理请求日志
- AOP实现日志和异常处理
- 利用AOP编程进行日志处理
- java jxl之Excel的读取
- php程序员进阶
- ADB 常用命令
- Android之使用Fragment无法相应onClick事件
- Thinking in Java:第七章-复用类
- controller日志AOP处理
- Hybrid App(混合模式移动应用) Windows系统下开发环境的搭建
- VS2013调用OpenCV实现放大图片
- Mysql5.7版本的下载与安装
- 学习韩顺平校内网项目时遇到的问题及解决方法
- VIM之高亮光标所在的行列
- 使用jQuery开发Todos实录
- 使用Ajax提交数据,进行前后台数据交互
- get&post