博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用spring aop 记录接口日志
阅读量:4314 次
发布时间:2019-06-06

本文共 5533 字,大约阅读时间需要 18 分钟。

spring配置文件中增加启用aop的配置

1 
2

 

切面类配置

1 package com.zchx.acvices;  2   3 import java.text.SimpleDateFormat;  4   5 import org.aspectj.lang.JoinPoint;  6 import org.aspectj.lang.ProceedingJoinPoint;  7 import org.aspectj.lang.annotation.After;  8 import org.aspectj.lang.annotation.AfterReturning;  9 import org.aspectj.lang.annotation.AfterThrowing; 10 import org.aspectj.lang.annotation.Around; 11 import org.aspectj.lang.annotation.Aspect; 12 import org.aspectj.lang.annotation.Before; 13 import org.aspectj.lang.annotation.Pointcut; 14 import org.slf4j.Logger; 15 import org.slf4j.LoggerFactory; 16 import org.springframework.stereotype.Component; 17  18 /** 19  * 切面日志记录 20  *  21  * @version V1.0 22  * @author songxiaotong 23  * @date 2018年2月6日 下午2:39:03 24  * @Description 25  */ 26 // 声明这是一个组件 27 @Component 28 // 声明这是一个切面Bean 29 @Aspect 30 public class Advices { 31  32     /** 33      * 日志记录工具 34      */ 35     private static final Logger LOGGER = LoggerFactory.getLogger(Advices.class); 36  37     /** 38      * 默认构造函数 39      */ 40     public Advices() { 41         LOGGER.debug("初始化日志切面"); 42     } 43  44     /** 45      * 配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点 46      * 

47 * 扫描com.sixeco下面的所有类 48 * 49 * @see [类、类#方法、类#成员] 50 */ 51 @Pointcut("execution(* com.zhichenhaixin..*.*(..)) or execution(* com.zchx..*.*(..))") 52 public void aspect() { 53 } 54 55 /** 56 * 配置前置通知,使用在方法aspect()上注册的切入点 57 *

58 * 同时接受JoinPoint切入点对象,可以没有该参数 59 * 60 * @param joinPoint 切入点 61 * @see [类、类#方法、类#成员] 62 */ 63 @Before("aspect()") 64 public void before(JoinPoint joinPoint) { 65 // LOGGER.debug("before {}", joinPoint.getSignature().toString()); 66 } 67 68 /** 69 * 配置后置通知,使用在方法aspect()上注册的切入点 70 *

71 *

72 * 73 * @param joinPoint 切入点 74 * @see [类、类#方法、类#成员] 75 */ 76 @After("aspect()") 77 public void after(JoinPoint joinPoint) { 78 // LOGGER.debug("after {}", joinPoint.getSignature().toString()); 79 } 80 81 /** 82 * 配置环绕通知,使用在方法aspect()上注册的切入点 83 *

84 * 记录方法开始到结束的耗时 85 * 86 * @param joinPoint 切入点 87 * @return Object 处理结果 88 * @throws Throwable 异常 89 * @see [类、类#方法、类#成员] 90 */ 91 @Around("aspect()") 92 public Object around(JoinPoint joinPoint) throws Throwable { 93 long start = System.currentTimeMillis(); 94 Object object = null; 95 try { 96 ProceedingJoinPoint tempJoinPoint = (ProceedingJoinPoint) joinPoint; 97 object = tempJoinPoint.proceed(); 98 long end = System.currentTimeMillis(); 99 // LOGGER.debug("around {} Use time : {} ms!",100 // joinPoint.getSignature().toString(), end - start);101 LOGGER.debug("计时时间:{} 耗时:{}毫秒 URI: {} 最大内存: {}m 已分配内存: {}m 已分配内存中的剩余空间: {}m 最大可用内存: {}m",102 new SimpleDateFormat("hh:mm:ss.SSS").format(start), end - start,103 joinPoint.getSignature().toString(), Runtime.getRuntime().maxMemory() / 1024 / 1024,104 Runtime.getRuntime().totalMemory() / 1024 / 1024, Runtime.getRuntime().freeMemory() / 1024 / 1024,105 (Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory()106 + Runtime.getRuntime().freeMemory()) / 1024 / 1024);107 } catch (Throwable e) {108 long end = System.currentTimeMillis();109 // LOGGER.debug("around {} Use time : {} ms with exception",110 // joinPoint.getSignature().toString(), end - start);111 112 LOGGER.debug("计时时间:{} 耗时:{}毫秒 URI: {} 最大内存: {}m 已分配内存: {}m 已分配内存中的剩余空间: {}m 最大可用内存: {}m",113 new SimpleDateFormat("hh:mm:ss.SSS").format(start), end - start,114 joinPoint.getSignature().toString(), Runtime.getRuntime().maxMemory() / 1024 / 1024,115 Runtime.getRuntime().totalMemory() / 1024 / 1024, Runtime.getRuntime().freeMemory() / 1024 / 1024,116 (Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory()117 + Runtime.getRuntime().freeMemory()) / 1024 / 1024);118 119 StackTraceElement[] s = e.getStackTrace();120 121 if (s.length >= 1) {122 StackTraceElement parentStack = s[0];123 LOGGER.error("发生异常 : 类名 >> {}, 函数名 >> {},问题产生行 >> {},类型 >> {}",124 new Object[] { parentStack.getClassName(), parentStack.getMethodName(),125 parentStack.getLineNumber(), e.getClass().getName() });126 }127 throw e;128 }129 return object;130 }131 132 /**133 *

<配置后置返回通知,使用在方法aspect()上注册的切入点134 *

135 * 136 * @param joinPoint 切入点137 * @see [类、类#方法、类#成员]138 */139 @AfterReturning("aspect()")140 public void afterReturn(JoinPoint joinPoint) {141 // LOGGER.debug("afterReturn {}", joinPoint.getSignature().toString());142 }143 144 /**145 * 配置抛出异常后通知,使用在方法aspect()上注册的切入点146 * 147 * @param joinPoint 切入点148 * @param ex 异常149 * @see [类、类#方法、类#成员]150 */151 @AfterThrowing(pointcut = "aspect()", throwing = "ex")152 public void afterThrow(JoinPoint joinPoint, Exception ex) {153 // LOGGER.debug("afterThrow {}", joinPoint.getSignature().toString());154 }155 }

 

转载于:https://www.cnblogs.com/songxiaotong/p/7228754.html

你可能感兴趣的文章
Linux系统安装出错后出现grub rescue的修复方法
查看>>
线段树模板整理
查看>>
[教程][6月4日更新]VMware 8.02虚拟机安装MAC lion 10.7.3教程 附送原版提取镜像InstallESD.iso!...
查看>>
[iOS问题归总]iPhone上传项目遇到的问题
查看>>
Python天天美味(总) --转
查看>>
Spring Framework tutorial
查看>>
【VS开发】win7下让程序默认以管理员身份运行
查看>>
【机器学习】Learning to Rank 简介
查看>>
Unity 使用实体类
查看>>
【转】通过文件锁实现,程序开始运行时,先判断文件是否存在,若存在则表明该程序已经在运行了,如果不存在就用open函数创建该文件,程序退出时关闭文件并删除文件...
查看>>
MySQL常见注意事项及优化
查看>>
流畅的Python (Fluent Python) —— 前言
查看>>
Jquery-menu-aim流畅的菜单滑动体验
查看>>
Jquery EasyUI修改行背景的两种方式
查看>>
生成器模式(Builder)C++实现
查看>>
Centos 7.5安装 Redis 5.0.0
查看>>
嵌入式Linux学习笔记(0)基础命令。——Arvin
查看>>
二分图匹配
查看>>
c++ 模板template
查看>>
javascript中的string对象
查看>>