背景:项目是老项目 , 而且比较旧为springmvc项目 。项目使用的框架为公司内部自己开发 , 目前已经没有框架的源码可供修改 , 配置文件写在底层框架内 , 可以看到但修改不到 。
目的是为了实现日志记录功能:
底层框架已经配置了aop 在ApplicationContext.xml
<aop:aspectj-autoproxy proxy-target-class="true" />
配置自动扫描包在
applicationContext-servlet.xml
<context:component-scan base-package="com.xxx"></context:component-scan>
自定义注解类
** * 自定义操作日志记录注解 ** */@Target({ ElementType.PARAMETER, ElementType.METHOD })@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface Log{/*** 模块*/public String title() default "";/*** 功能*/public BusinessType businessType() default BusinessType.OTHER;/*** 操作人类别*/public OperatorType operatorType() default OperatorType.MANAGE;/*** 是否保存请求的参数*/public boolean isSaveRequestData() default true;}
自定义切面类
@Aspect@Componentpublic class LogAspect {private static final Logger log = LoggerFactory.getLogger(LogAspect.class);// 配置织入点@Pointcut("within(com.transin..*) && @annotation(com.transin.waste.annotation.Log)")public void logPointCut(){}/*** 处理完请求后执行** @param joinPoint 切点*/@AfterReturning(pointcut = "logPointCut()", returning = "jsonResult")public void doAfterReturning(JoinPoint joinPoint, Object jsonResult){handleLog(joinPoint, null, jsonResult);}/*** 拦截异常操作** @param joinPoint 切点* @param e 异常*/@AfterThrowing(value = https://www.isolves.com/it/cxkf/kj/2021-08-11/"logPointCut()", throwing = "e")public void doAfterThrowing(JoinPoint joinPoint, Exception e){handleLog(joinPoint, e, null);}protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult){try{// 获得注解Log controllerLog = getAnnotationLog(joinPoint);if (controllerLog == null){return;}// 获取当前的用户LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());// *========数据库日志=========*//SysOperLog operLog = new SysOperLog();operLog.setStatus(BusinessStatus.SUCCESS.ordinal());// 请求的地址String ip = IpUtils.getIpAddr(ServletUtils.getRequest());operLog.setOperIp(ip);// 返回参数operLog.setJsonResult(JSON.toJSONString(jsonResult));operLog.setOperUrl(ServletUtils.getRequest().getRequestURI());if (loginUser != null){operLog.setOperName(loginUser.getUsername());}if (e != null){operLog.setStatus(BusinessStatus.FAIL.ordinal());operLog.setErrorMsg(e.getMessage().substring(0, 2000));}// 设置方法名称String className = joinPoint.getTarget().getClass().getName();String methodName = joinPoint.getSignature().getName();operLog.setMethod(className + "." + methodName + "()");// 设置请求方式operLog.setRequestMethod(ServletUtils.getRequest().getMethod());// 处理设置注解上的参数getControllerMethodDescription(joinPoint, controllerLog, operLog);// 保存数据库AsyncManager.me().execute(AsyncFactory.recordOper(operLog));}catch (Exception exp){// 记录本地异常日志log.error("==前置通知异常==");log.error("异常信息:{}", exp.getMessage());exp.printStackTrace();}}/*** 获取注解中对方法的描述信息 用于Controller层注解** @param log 日志* @param operLog 操作日志* @throws Exception*/public void getControllerMethodDescription(JoinPoint joinPoint, Log log, SysOperLog operLog) throws Exception{// 设置action动作operLog.setBusinessType(log.businessType().ordinal());// 设置标题operLog.setTitle(log.title());// 设置操作人类别operLog.setOperatorType(log.operatorType().ordinal());// 是否需要保存request , 参数和值if (log.isSaveRequestData()){// 获取参数的信息 , 传入到数据库中 。setRequestValue(joinPoint, operLog);}}/*** 获取请求的参数 , 放到log中** @param operLog 操作日志* @throws Exception 异常*/private void setRequestValue(JoinPoint joinPoint, SysOperLog operLog) throws Exception{String requestMethod = operLog.getRequestMethod();if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)){String params = argsArrayToString(joinPoint.getArgs());operLog.setOperParam(params.substring(0, 2000));}else{Map, ?> paramsMap = (Map, ?>) ServletUtils.getRequest().getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);operLog.setOperParam(paramsMap.toString().substring( 0, 2000));}}/*** 是否存在注解 , 如果存在就获取*/private Log getAnnotationLog(JoinPoint joinPoint) throws Exception{Signature signature = joinPoint.getSignature();MethodSignature methodSignature = (MethodSignature) signature;Method method = methodSignature.getMethod();if (method != null){return method.getAnnotation(Log.class);}return null;}/*** 参数拼装*/private String argsArrayToString(Object[] paramsArray){String params = "";if (paramsArray != null && paramsArray.length > 0){for (int i = 0; i
推荐阅读
- 2021年低预算高性价比笔记本电脑选购指南
- 如何判定新买的笔记本是否是全新机器
- 2020年工伤一次性补助赔偿标准表 2020年工伤死亡一次性补助金标准
- C罗荣誉记录多少 c罗保持的记录总和
- 鸡胗皮磨粉一次吃多少
- a股大盘涨停板历史记录 a股大盘涨停
- 2021选购笔记本电脑须知
- 大一新生笔记本电脑推荐2021
- MySQL单表最大记录数超过多少时性能会严重下降?
- MySQL死锁分析:记一次因索引合并导致的MySQL死锁分析过程