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

 <!-- 增加aop 自动代理配置 -->
<aop:aspectj-autoproxy />

切面类配置

 package com.zchx.acvices;

 import java.text.SimpleDateFormat;

 import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; /**
* 切面日志记录
*
* @version V1.0
* @author songxiaotong
* @date 2018年2月6日 下午2:39:03
* @Description
*/
// 声明这是一个组件
@Component
// 声明这是一个切面Bean
@Aspect
public class Advices { /**
* 日志记录工具
*/
private static final Logger LOGGER = LoggerFactory.getLogger(Advices.class); /**
* 默认构造函数
*/
public Advices() {
LOGGER.debug("初始化日志切面");
} /**
* 配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点
* <p>
* 扫描com.sixeco下面的所有类
*
* @see [类、类#方法、类#成员]
*/
@Pointcut("execution(* com.zhichenhaixin..*.*(..)) or execution(* com.zchx..*.*(..))")
public void aspect() {
} /**
* 配置前置通知,使用在方法aspect()上注册的切入点
* <p>
* 同时接受JoinPoint切入点对象,可以没有该参数
*
* @param joinPoint 切入点
* @see [类、类#方法、类#成员]
*/
@Before("aspect()")
public void before(JoinPoint joinPoint) {
// LOGGER.debug("before {}", joinPoint.getSignature().toString());
} /**
* 配置后置通知,使用在方法aspect()上注册的切入点
* <p>
* </p>
*
* @param joinPoint 切入点
* @see [类、类#方法、类#成员]
*/
@After("aspect()")
public void after(JoinPoint joinPoint) {
// LOGGER.debug("after {}", joinPoint.getSignature().toString());
} /**
* 配置环绕通知,使用在方法aspect()上注册的切入点
* <p>
* 记录方法开始到结束的耗时
*
* @param joinPoint 切入点
* @return Object 处理结果
* @throws Throwable 异常
* @see [类、类#方法、类#成员]
*/
@Around("aspect()")
public Object around(JoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
Object object = null;
try {
ProceedingJoinPoint tempJoinPoint = (ProceedingJoinPoint) joinPoint;
object = tempJoinPoint.proceed();
long end = System.currentTimeMillis();
// LOGGER.debug("around {} Use time : {} ms!",
// joinPoint.getSignature().toString(), end - start);
LOGGER.debug("计时时间:{} 耗时:{}毫秒 URI: {} 最大内存: {}m 已分配内存: {}m 已分配内存中的剩余空间: {}m 最大可用内存: {}m",
new SimpleDateFormat("hh:mm:ss.SSS").format(start), end - start,
joinPoint.getSignature().toString(), Runtime.getRuntime().maxMemory() / 1024 / 1024,
Runtime.getRuntime().totalMemory() / 1024 / 1024, Runtime.getRuntime().freeMemory() / 1024 / 1024,
(Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory()
+ Runtime.getRuntime().freeMemory()) / 1024 / 1024);
} catch (Throwable e) {
long end = System.currentTimeMillis();
// LOGGER.debug("around {} Use time : {} ms with exception",
// joinPoint.getSignature().toString(), end - start); LOGGER.debug("计时时间:{} 耗时:{}毫秒 URI: {} 最大内存: {}m 已分配内存: {}m 已分配内存中的剩余空间: {}m 最大可用内存: {}m",
new SimpleDateFormat("hh:mm:ss.SSS").format(start), end - start,
joinPoint.getSignature().toString(), Runtime.getRuntime().maxMemory() / 1024 / 1024,
Runtime.getRuntime().totalMemory() / 1024 / 1024, Runtime.getRuntime().freeMemory() / 1024 / 1024,
(Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory()
+ Runtime.getRuntime().freeMemory()) / 1024 / 1024); StackTraceElement[] s = e.getStackTrace(); if (s.length >= 1) {
StackTraceElement parentStack = s[0];
LOGGER.error("发生异常 : 类名 >> {}, 函数名 >> {},问题产生行 >> {},类型 >> {}",
new Object[] { parentStack.getClassName(), parentStack.getMethodName(),
parentStack.getLineNumber(), e.getClass().getName() });
}
throw e;
}
return object;
} /**
* <配置后置返回通知,使用在方法aspect()上注册的切入点
* <p>
*
* @param joinPoint 切入点
* @see [类、类#方法、类#成员]
*/
@AfterReturning("aspect()")
public void afterReturn(JoinPoint joinPoint) {
// LOGGER.debug("afterReturn {}", joinPoint.getSignature().toString());
} /**
* 配置抛出异常后通知,使用在方法aspect()上注册的切入点
*
* @param joinPoint 切入点
* @param ex 异常
* @see [类、类#方法、类#成员]
*/
@AfterThrowing(pointcut = "aspect()", throwing = "ex")
public void afterThrow(JoinPoint joinPoint, Exception ex) {
// LOGGER.debug("afterThrow {}", joinPoint.getSignature().toString());
}
}

使用spring aop 记录接口日志的更多相关文章

  1. Spring aop 记录操作日志 Aspect

    前几天做系统日志记录的功能,一个操作调一次记录方法,每次还得去收集参数等等,太尼玛烦了.在程序员的世界里,当你的一个功能重复出现多次,就应该想想肯定有更简单的实现方法.于是果断搜索各种资料,终于搞定了 ...

  2. Spring aop 记录操作日志 Aspect 自定义注解

    时间过的真快,转眼就一年了,没想到随手写的笔记会被这么多人浏览,不想误人子弟,于是整理了一个优化版,在这里感谢智斌哥提供的建议和帮助,话不多说,进入正题 所需jar包 :spring4.3相关联以及a ...

  3. SpringBoot应用中使用AOP记录接口访问日志

    SpringBoot应用中使用AOP记录接口访问日志 本文主要讲述AOP在mall项目中的应用,通过在controller层建一个切面来实现接口访问的统一日志记录. AOP AOP为Aspect Or ...

  4. Spring Boot中使用AOP记录请求日志

    这周看别人写的springboot后端代码中有使用AOP记录请求日志,以前没接触过,因此学习下. 一.AOP简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编 ...

  5. SpringBoot 使用AOP记录接口访问日志

    文章来源:https://macrozheng.github.io/mall-learning/#/technology/aop_log AOP AOP为Aspect Oriented Program ...

  6. [编码实践]SpringBoot实战:利用Spring AOP实现操作日志审计管理

    设计原则和思路: 元注解方式结合AOP,灵活记录操作日志 能够记录详细错误日志为运营以及审计提供支持 日志记录尽可能减少性能影响 操作描述参数支持动态获取,其他参数自动记录. 1.定义日志记录元注解, ...

  7. log4j+AOP 记录错误日志信息到文件中

    AOP 采用异常通知切入,把指定包的异常记录到日志文件. 先看log4j.properties ,控制台输出的是普通信息, 文件输出的是异常信息. log4j.rootLogger=DEBUG, Co ...

  8. springboot 2.x整合redis,spring aop实现接口缓存

    pox.xml: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  9. 使用SpringBoot AOP 记录操作日志、异常日志

    平时我们在做项目时经常需要对一些重要功能操作记录日志,方便以后跟踪是谁在操作此功能:我们在操作某些功能时也有可能会发生异常,但是每次发生异常要定位原因我们都要到服务器去查询日志才能找到,而且也不能对发 ...

随机推荐

  1. Fink| 实时热门商品

    HotNItems 拓展需求:实时统计双十一下单量,实时统计成交额,实时查看锅炉温度变化曲线,每个5分钟看一下过去一个小时温度变化曲线, 涉及到的技术点:sliding window.Watermar ...

  2. pyqt5环境变量踩坑记

    之前用一个cmd脚本 wmic ENVIRONMENT create name="QT_QPA_PLATFORM_PLUGIN_PATH",username="<s ...

  3. CF 938G Shortest Path Queries

    又到了喜闻乐见的写博客清醒时间了233,今天做的依然是线段树分治 这题算是经典应用了吧,假的动态图(可离线)问题 首先不难想到对于询问的时间进行线段树分治,这样就可以把每一条边出现的时间区间扔进线段树 ...

  4. 小明工具箱<Excel 插件><VSTO 插件>

    当前版本:1.0.42.7118(更新日期:2019年6月28日) 下载地址:点击下载 功能和简介: 本程序为 Excel 2010 版本以上的插件,含以下功能: 拆分工作簿:将一个或多个工作簿中的每 ...

  5. VMWare 怎样复制/copy部署好的一台虚拟机

    1:修改 vi /ect/udev/rules.d/70-persisten-net.rules 2:执行命令:uuidgen eth0 3:vi /etc/sysconfig/network-scr ...

  6. 扩展centos7.4虚拟机磁盘大小

    虚拟机分配磁盘40GB,实际系统分区只用了20GB,需要手工扩展到40GB,操作方法如下: 查看磁盘信息(确认主分区只有17GB):[root@test-web1 ~]# df -hFilesyste ...

  7. COMP222 - 2019

    COMP222 - 2019 - Second CA AssignmentIndividual courseworkTrain Deep Learning AgentsAssessment Infor ...

  8. javascript中的发布订阅模式与观察者模式

    这里了解一下JavaScript中的发布订阅模式和观察者模式,观察者模式是24种基础设计模式之一. 设计模式的背景 设计模式并非是软件开发的专业术语,实际上设计模式最早诞生于建筑学. 设计模式的定义是 ...

  9. kali渗透综合靶机(六)--FristiLeaks靶机

    kali渗透综合靶机(六)--FristiLeaks靶机 靶机地址下载:https://download.vulnhub.com/fristileaks/FristiLeaks_1.3.ova 一.主 ...

  10. JS运算符类型

    一.运算符类型 1.算术运算符: 用于各类数值运算,包括加(+).减(-).乘(*).除(/).求余(或称模运算,%).自增(++).自减(--)共七种. 2.关系运算符: 用于比较运算.包括大于(& ...