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());
}
}

最新文章

  1. bootstrap-select js jQuery控制select属性变化
  2. Java Annotation自定义注解详解
  3. 黑马程序员——OC语言Foundation框架 (2) NSArray NSSet NSDictionary\NSMutableDictionary
  4. 怎么学习C++?
  5. 思考之一——PM(Project Manager)
  6. 如何禁止在DBGRID末位自动添加一行记录
  7. js 对象与函数的区别
  8. DOS批处理命令-引数取得
  9. ar技术序章-SDK介绍和选择
  10. C#截取指定字符串函数
  11. Python Tutorial 学习(十)-- Brief Tour of the Standard Library
  12. Unity UGUI实现图文混排
  13. ruby rails_autolink不能加载的原因
  14. MUI之App开发
  15. windows下启动mysql服务
  16. 云服务jdk 升级为 OpenJDK11
  17. 洛谷 3784(bzoj 4913) [SDOI2017]遗忘的集合——多项式求ln+MTT
  18. Python日志记录(logging)
  19. 解决tomcat启动慢
  20. AJAX的中文乱码问题

热门文章

  1. 7.Python网络编程_多线程共享全局变量问题
  2. 【cf741】D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(dsu on tree)
  3. BASIC合集
  4. Appium自动化WebView中元素的操作
  5. React、Vue、Angular对比 ---- 介绍及优缺点
  6. linux常用终端命令
  7. NAT技术详解
  8. Aug.2019_Memory
  9. 全面理解UE4委托
  10. EF直接更新数据(不需查询)