Spring aop拦截springmvc的controller请求方法,添加日志和统计方法执行时间
来源:互联网 发布:英雄联盟源码 编辑:程序博客网 时间:2024/06/06 23:18
前言
系统最近莫名挂了三次,查看log日志没找到原因,因为只有少数功能日志输出,大部分功能都没记录日志。打算通过spring aop来给springmvc的controller层的方法加日志,进入方法,方法执行完都记录日志,同时记录方法执行的耗时。日志输出级别为debug,通过log4j的分级别输出日志到不同的文件,下面记录下aop拦截的步骤。
1.applicationContext.xml中的配置
<!--开启注解--><context:annotation-config /><!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 --><context:component-scan base-package="com.***"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan><!-- 开启自动切面代理 --><aop:aspectj-autoproxy />2.aspect类的编写
@Before 进入方法打印带包路径的方法名和当前时间
@After 方法执行完打印带包路径的方法名和当前时间
@Around 大于常量的毫秒数,打印方法执行时间
@Aspect@Componentpublic class LogInterceptor { private final Logger logger = Logger.getLogger(LogInterceptor.class); // 一分钟,即1000ms private static final long ONE_MINUTE = 1000; @Pointcut("execution(public * com.sully..controller..*.*(..))") public void myMethod(){}; /** * 进入方法后打印日志 * @param joinPoint */ @Before("myMethod()") public void before(JoinPoint joinPoint) { logger.debug(this.getMethodName(joinPoint)+" start "+ DateUtil.getDateTime(new Date())); } /** * 方法结束打印日志 * @param joinPoint */ @After("myMethod()") public void after(JoinPoint joinPoint) { logger.debug(this.getMethodName(joinPoint)+" after"+ DateUtil.getDateTime(new Date())); } @Around("execution(* com.sully..controller..*.*(..))") public Object processLog(ProceedingJoinPoint joinPoint) throws Throwable { // 定义返回对象、得到方法需要的参数 Object obj = null; Object[] args = joinPoint.getArgs(); long startTime = System.currentTimeMillis(); try { obj = joinPoint.proceed(args); } catch (Throwable e) { logger.error("统计某方法执行耗时环绕通知出错", e); } // 获取执行的方法名 long endTime = System.currentTimeMillis(); MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String methodName = signature.getDeclaringTypeName() + "." + signature.getName(); // 打印耗时的信息 this.printExecTime(methodName, startTime, endTime); return obj; } /** * 打印方法执行耗时的信息,如果超过了一定的时间,才打印 * @param methodName * @param startTime * @param endTime */ private void printExecTime(String methodName, long startTime, long endTime) { long diffTime = endTime - startTime; if (diffTime > ONE_MINUTE) { logger.debug( methodName + " 方法执行耗时:" + diffTime + " ms"); } } /** * 获取方法名(类的详细包路径) * @param joinPoint * @return */ private String getMethodName(JoinPoint joinPoint){ return joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName(); } /** * AfterReturning 拦截执行 */ @AfterReturning("execution(public * com.sully.finance..controller..*.*(..))") public void AfterReturning() { logger.debug("method AfterReturning"); } /** * AfterThrowing 拦截执行 */ @AfterThrowing("execution(public * com.sully.finance..controller..*.*(..))") public void AfterThrowing() { logger.debug("method AfterThrowing"); }}
3.步骤2中的日志打印都是debug级别,分级别输出日志就好
阅读全文
0 1
- Spring aop拦截springmvc的controller请求方法,添加日志和统计方法执行时间
- spring Aop拦截controller方法
- Spring AOP - 注解实现统计service中方法的执行时间
- 使用Spring AOP来统计方法的执行时间
- 使用Spring AOP来统计方法的执行时间
- 使用Spring AOP来统计方法的执行时间
- Spring AOP无法拦截Controller中的方法
- SpringMVC拦截Controller方法
- SpringMVC的AOP拦截controller
- Sping AOP用AspectJ注解的方式拦截不到SpringMVC的controller方法?
- 日志AOP拦截Controller
- SpringMVC关于AOP拦截controller的注意事项
- SpringMVC关于AOP拦截controller的注意事项
- spring拦截器 拦截springMVC的controller
- 使用Spring的AOP实现接口方法执行时间记录
- spring security基于aop的方法拦截
- spring aop 实现方法执行时间监控
- spring aop 实现方法执行时间监控
- 基于EasyNVR实现RTSP/Onvif监控摄像头Web无插件化直播监控
- less-Extend(扩展)
- 秋实大哥与线段树 UESTC
- Karen and Coffee(前缀和,大量查询)
- 轻舟已过
- Spring aop拦截springmvc的controller请求方法,添加日志和统计方法执行时间
- Golang goroutine协程(一) 编写第一个并发处理的程序
- 人见人爱A^B HDU
- 51Nod-TalkingData数据科学精英夏令营挑战赛-D-数据流中的算法-众数
- 深入理解Java中的String
- 坚持#第185天~看到张亦驰发的狗粮,忍不住要奋斗了
- 重庆轻舟已过
- virtualenv创建隔离的Python环境
- [BZOJ4012][HNOI2015]开店-树链剖分-主席树