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. 在Rust中,cargo使用国内镜像源

    一个编程语言依赖包管理的普通问题. cargo解决得比较优雅. 一,新建$HOME/.cargo/config文件 [source.crates-io] registry = "https: ...

  2. 初学JavaScript正则表达式(二)

    正则表达式的实例化与标识符 字面量: var reg = /\bis\b/g // \b--字符边界 g全文搜索 查找单词为is的字符 He is a boy. IS He? 构造函数: var re ...

  3. phpstudy配置虚拟域名

    之前有一篇使用xampp配置虚拟域名,但是不同公司使用的集成环境不同,(xampp是我自己用的,别误解(><) !)这次使用的phpstudy,相比较而言,phpstudy更简单一点 首先 ...

  4. luoguP4069 [SDOI2016]游戏

    题意 显然书剖套李超树. 考虑怎么算函数值: 设\((x,y)\)的\(lca\)为\(z\),我们插一条斜率为\(k\),截距为\(b\)的线段. \((x,z)\)上的点\(u\): \(f(u) ...

  5. QDialog 设置成圆角

    void paintEvent(QPaintEvent *event) { Q_UNUSED(event); QBitmap bmp(this->size()); bmp.fill(); QPa ...

  6. R语言- 实验报告 - 利用R语言脚本与Java相互调用

    一. 实训内容 利用R语言对Java项目程序进行调用,本实验包括利用R语言对java的.java文件进行编译和执行输出. 在Java中调用R语言程序.本实验通过eclipse编写Java程序的方式,调 ...

  7. java.util.concurrent各组件分析 一 sun.misc.Unsafe

    java.util.concurrent各组件分析 一 sun.misc.Unsafe 说到concurrent包也叫并发包,该包下主要是线程操作,方便的进行并发编程,提到并发那么锁自然是不可缺少的, ...

  8. 1+x 证书 Web 前端开发 JavaScript 专项练习

    官方QQ群 1+x 证书 Web 前端开发 JavaScript 专项练习 http://blog.zh66.club/index.php/archives/198/

  9. 【测试方法】Web测试中bug定位基本方法

    知识总结:Web测试中bug定位基本方法 涉及知识点:测试方法 在web测试过程中,经常会遇到页面中内容或数据显示错误,甚至不显示,第一反应就是BUG,没错,确实是BUG.进一步了解这个BUG的问题出 ...

  10. 坑爹的京东E卡(京东E卡的正确使用方式)

      前言    今年中秋公司发了200的京东E卡(下面简称礼品卡,京东简称jd)这让喜欢在jd自营购买商品的我很是开心,    兴致勃勃打开官网,当我选好商品准备结算时却发现礼品卡无法使用.    后 ...