使用Spring的AOP实现接口方法执行时间记录
项目使用RPC提供的内部服务,需要监控每个接口方法的调用情况以及响应时间,如果接口方法开始和结束时都计时并将两个时间相减得到响应时间,势必对代码的入侵太大。使用AOP刚好能很优雅的解决这个问题!
1.MethodTimeAdvice.java 用来记录时间
- package yourpackage.utils;
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- import org.apache.commons.lang.StringUtils;
- import org.apache.commons.lang.time.StopWatch;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- public class MethodTimeAdvice implements MethodInterceptor {
- private static final Logger Log = LoggerFactory
- .getLogger(MethodTimeAdvice.class);
- @Override
- public Object invoke(MethodInvocation invocation) throws Throwable {
- // 用 commons-lang 提供的 StopWatch 计时
- StopWatch clock = new StopWatch();
- clock.start(); // 计时开始
- Object result = invocation.proceed();
- clock.stop(); // 计时结束
- // 方法参数类型,转换成简单类型
- Class[] params = invocation.getMethod().getParameterTypes();
- String[] simpleParams = new String[params.length];
- for (int i = 0; i < params.length; i++) {
- simpleParams[i] = params[i].getSimpleName();
- }
- Object[] args = invocation.getArguments();
- Log.info("Takes:" + clock.getTime() + " ms ["
- + invocation.getThis().getClass().getName() + "."
- + invocation.getMethod().getName() + "("
- + StringUtils.join(simpleParams, ",") + ")("
- + StringUtils.join(args, ",") + ")] ");
- return result;
- }
- }
3.applicationContext.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
- default-lazy-init="true">
- <aop:aspectj-autoproxy />
- <bean id="methodTimeAdvice" class="cn.hiluo.openfire.utils.MethodTimeAdvice" />
- <aop:config>
- <!-- 用 AspectJ 的语法定义 Pointcut,这里拦截 service 包中的所有方法 -->
- <aop:advisor id="methodTimeLog" advice-ref="methodTimeAdvice"
- pointcut="execution(* yourpackage.service.impl..*.*(..))" />
- </aop:config>
- <bean id="yourservice" class="**.impl.yourServiceImpl">
- </bean>
- 。。。。。yourbeans
- </beans>
4.注意需要加入Spring相关包,比较容易遗漏的:org.springframework.aop-3.0.6.RELEASE.jar,org.springframework.aspects-3.0.6.RELEASE.jar,aspectjweaver-1.5.3.jar,aopalliance-1.0.jar
5.applicationContext.xml注意xml中和AOP相关的dtd的引入,还要注意pointcut="execution(* yourpackage.service.impl..*.*(..))" 的配置语法
6.最后在info.log里面可以看到如上图的输出,最后再用shell统计即可。
参考:
http://jportal.iteye.com/blog/945725
http://www.java63.com/spring/configure_implement_aop.html
使用Spring的AOP实现接口方法执行时间记录相关推荐
- Spring之AOP系列--将方法上的注解做为切点(用@Around)
原文网址:Spring之AOP系列--将方法上的注解做为切点(用@Around)_IT利刃出鞘的博客-CSDN博客 简介 说明 本文介绍Spring(SpringBoot)的AOP的用 ...
- 利用AOP实现对方法执行时间的统计
由于项目主管提出统计每个方法执行的具体时间,打印到日志以后方便分析问题.这个也是很常用的办法,但是我们调用的外部模块非常多,每个地方都加上一个时间统计就显得非常难看了.所以这个事情当然还是交给spri ...
- SpringBoot中使用AOP打印接口日志的方法(转载)
前言 AOP 是 Aspect Oriented Program (面向切面)的编程的缩写.他是和面向对象编程相对的一个概念.在面向对象的编程中,我们倾向于采用封装.继承.多态等概念,将一个个的功能在 ...
- Spring AOP根据JdbcTemplate方法名动态设置数据源
2019独角兽企业重金招聘Python工程师标准>>> 说明:现在的场景是,采用数据库(Mysql)复制(binlog)的方式在两台不同服务器部署并配置主从(Master-Slave ...
- Spring框架(IoC、AOP面向接口切面)
新建一个Maven工程 Spring框架是由于软件开发的复杂性而创建的.Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spring的用途不仅仅限于服务器端的开发. ...
- Spring Boot AOP处理方法的入参和返回值
前言 IOC和AOP是Spring 中最重要的两个模块.这里练习一下如何使用Spring Boot AOP处理方法的入参和返回值. Spring AOP的简单介绍: AOP(Aspect-Orient ...
- spring AOP对父类方法加强分析
spring AOP可以对方法进行加强,就是在方法前执行一些想要的事情,执行方法后想执行一些信息,原理就是利用动态代理,具体不在阐述 今天要讨论的是一个springBean继承了父类,在父类里进行了方 ...
- Java开发【Spring之AOP详解(xml--注解->方法增强、事务管理(声明事务的实现))】
文章目录 引入 一.AOP概述 1.什么是AOP 2.AOP的优势及使用场景 3.AOP实现原理 二.代理模式 1.代理模式概念 2.代理模式分类 3.静态代理演示 定义HouseAgencyComp ...
- Spring的aspect无法拦截有注解的jdk代理的接口方法的原因
Spring的aspect无法拦截有注解的jdk代理的接口方法的原因 我github博客地址 背景 项目A中需要多数据源的实现,比如UserDao.getAllUserList() 需要从readon ...
最新文章
- Angularjs $scope 里面的$apply 方法 和 $watch 方法
- 谱聚类方法-MATLAB
- es6 数组合并_13个不low的JS数组操作,你需要知道一下
- w7重启计算机打印机无法使用,win7系统电脑重启打印机服务就会被关闭的解决方法...
- 10分钟Get拥抱无服务的正确姿势
- Hyperledger Fabric学习笔记(四)- fabric单机部署 solo 版
- Tutorials 使用窗口功能分析信息
- PHP下载文件函数封装及下载大文件解决方案
- SVPWM matlab建模
- G1调优实践日记--G1HeapWastePercent和InitiatingHeapOccupancyPercent的应用
- 在Mac OS系统下安装Java
- 《批量处理图片》批量把文件夹中的图片放到Excel中-Excel批量上传图片
- 中科院计算所陈云霁:深度学习芯片剩下的只是工程问题,我们要起航探索新方向
- 微信小程序|考试系统|基于微信小程序和SpringBoot+VUE的智能在线考试系统毕业设计
- 未将对象引用设置到对象的实例。
- Unity的Dots技术入门
- 国家发布电子病历共享文档规范
- Qt调起外部应用并嵌入界面
- 百度UNIT使用的步骤与自己的一点简单理解(下)
- Car Key是什么?
热门文章
- Python遥感图像处理应用篇(二十二):Python+GDAL 批量等距离裁剪影像-续
- 如何编写Java单元测试(TC)?
- WideCharToMultiByte
- rk3128-android5.1-ota升级清除data分区
- 在线调色工具 在线CSS工具 CSS设计展示网站汇总
- nhibernate的关系
- Windows如何清除已保存的FTP密码
- bicq php,BICQ 1、首先 软件跟QQ和MSN有很多类似的地方。因为现在 用户群在使用 WEB(ASP,PHP,...) 256万源代码下载- www.pudn.com...
- (浪漫七夕版)听七夕浪漫故事,玩酷炫浏览器
- spring task定时任务(个人笔记,非教程)