项目使用RPC提供的内部服务,需要监控每个接口方法的调用情况以及响应时间,如果接口方法开始和结束时都计时并将两个时间相减得到响应时间,势必对代码的入侵太大。使用AOP刚好能很优雅的解决这个问题!

1.MethodTimeAdvice.java 用来记录时间

Java代码  
  1. package yourpackage.utils;
  2. import org.aopalliance.intercept.MethodInterceptor;
  3. import org.aopalliance.intercept.MethodInvocation;
  4. import org.apache.commons.lang.StringUtils;
  5. import org.apache.commons.lang.time.StopWatch;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. public class MethodTimeAdvice implements MethodInterceptor {
  9. private static final Logger Log = LoggerFactory
  10. .getLogger(MethodTimeAdvice.class);
  11. @Override
  12. public Object invoke(MethodInvocation invocation) throws Throwable {
  13. // 用 commons-lang 提供的 StopWatch 计时
  14. StopWatch clock = new StopWatch();
  15. clock.start(); // 计时开始
  16. Object result = invocation.proceed();
  17. clock.stop(); // 计时结束
  18. // 方法参数类型,转换成简单类型
  19. Class[] params = invocation.getMethod().getParameterTypes();
  20. String[] simpleParams = new String[params.length];
  21. for (int i = 0; i < params.length; i++) {
  22. simpleParams[i] = params[i].getSimpleName();
  23. }
  24. Object[] args = invocation.getArguments();
  25. Log.info("Takes:" + clock.getTime() + " ms ["
  26. + invocation.getThis().getClass().getName() + "."
  27. + invocation.getMethod().getName() + "("
  28. + StringUtils.join(simpleParams, ",") + ")("
  29. + StringUtils.join(args, ",") + ")] ");
  30. return result;
  31. }
  32. }

3.applicationContext.xml

Xml代码  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
  9. default-lazy-init="true">
  10. <aop:aspectj-autoproxy />
  11. <bean id="methodTimeAdvice" class="cn.hiluo.openfire.utils.MethodTimeAdvice" />
  12. <aop:config>
  13. <!-- 用 AspectJ 的语法定义 Pointcut,这里拦截 service 包中的所有方法 -->
  14. <aop:advisor id="methodTimeLog" advice-ref="methodTimeAdvice"
  15. pointcut="execution(* yourpackage.service.impl..*.*(..))" />
  16. </aop:config>
  17. <bean id="yourservice" class="**.impl.yourServiceImpl">
  18. </bean>
  19. 。。。。。yourbeans
  20. </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实现接口方法执行时间记录相关推荐

  1. Spring之AOP系列--将方法上的注解做为切点(用@Around)

    原文网址:Spring之AOP系列--将方法上的注解做为切点(用@Around)_IT利刃出鞘的博客-CSDN博客 简介 说明         本文介绍Spring(SpringBoot)的AOP的用 ...

  2. 利用AOP实现对方法执行时间的统计

    由于项目主管提出统计每个方法执行的具体时间,打印到日志以后方便分析问题.这个也是很常用的办法,但是我们调用的外部模块非常多,每个地方都加上一个时间统计就显得非常难看了.所以这个事情当然还是交给spri ...

  3. SpringBoot中使用AOP打印接口日志的方法(转载)

    前言 AOP 是 Aspect Oriented Program (面向切面)的编程的缩写.他是和面向对象编程相对的一个概念.在面向对象的编程中,我们倾向于采用封装.继承.多态等概念,将一个个的功能在 ...

  4. Spring AOP根据JdbcTemplate方法名动态设置数据源

    2019独角兽企业重金招聘Python工程师标准>>> 说明:现在的场景是,采用数据库(Mysql)复制(binlog)的方式在两台不同服务器部署并配置主从(Master-Slave ...

  5. Spring框架(IoC、AOP面向接口切面)

    新建一个Maven工程 Spring框架是由于软件开发的复杂性而创建的.Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情.然而,Spring的用途不仅仅限于服务器端的开发. ...

  6. Spring Boot AOP处理方法的入参和返回值

    前言 IOC和AOP是Spring 中最重要的两个模块.这里练习一下如何使用Spring Boot AOP处理方法的入参和返回值. Spring AOP的简单介绍: AOP(Aspect-Orient ...

  7. spring AOP对父类方法加强分析

    spring AOP可以对方法进行加强,就是在方法前执行一些想要的事情,执行方法后想执行一些信息,原理就是利用动态代理,具体不在阐述 今天要讨论的是一个springBean继承了父类,在父类里进行了方 ...

  8. Java开发【Spring之AOP详解(xml--注解->方法增强、事务管理(声明事务的实现))】

    文章目录 引入 一.AOP概述 1.什么是AOP 2.AOP的优势及使用场景 3.AOP实现原理 二.代理模式 1.代理模式概念 2.代理模式分类 3.静态代理演示 定义HouseAgencyComp ...

  9. Spring的aspect无法拦截有注解的jdk代理的接口方法的原因

    Spring的aspect无法拦截有注解的jdk代理的接口方法的原因 我github博客地址 背景 项目A中需要多数据源的实现,比如UserDao.getAllUserList() 需要从readon ...

最新文章

  1. Angularjs $scope 里面的$apply 方法 和 $watch 方法
  2. 谱聚类方法-MATLAB
  3. es6 数组合并_13个不low的JS数组操作,你需要知道一下
  4. w7重启计算机打印机无法使用,win7系统电脑重启打印机服务就会被关闭的解决方法...
  5. 10分钟Get拥抱无服务的正确姿势
  6. Hyperledger Fabric学习笔记(四)- fabric单机部署 solo 版
  7. Tutorials 使用窗口功能分析信息
  8. PHP下载文件函数封装及下载大文件解决方案
  9. SVPWM matlab建模
  10. G1调优实践日记--G1HeapWastePercent和InitiatingHeapOccupancyPercent的应用
  11. 在Mac OS系统下安装Java
  12. 《批量处理图片》批量把文件夹中的图片放到Excel中-Excel批量上传图片
  13. 中科院计算所陈云霁:深度学习芯片剩下的只是工程问题,我们要起航探索新方向
  14. 微信小程序|考试系统|基于微信小程序和SpringBoot+VUE的智能在线考试系统毕业设计
  15. 未将对象引用设置到对象的实例。
  16. Unity的Dots技术入门
  17. 国家发布电子病历共享文档规范
  18. Qt调起外部应用并嵌入界面
  19. 百度UNIT使用的步骤与自己的一点简单理解(下)
  20. Car Key是什么?

热门文章

  1. Python遥感图像处理应用篇(二十二):Python+GDAL 批量等距离裁剪影像-续
  2. 如何编写Java单元测试(TC)?
  3. WideCharToMultiByte
  4. rk3128-android5.1-ota升级清除data分区
  5. 在线调色工具 在线CSS工具 CSS设计展示网站汇总
  6. nhibernate的关系
  7. Windows如何清除已保存的FTP密码
  8. bicq php,BICQ   1、首先 软件跟QQ和MSN有很多类似的地方。因为现在 用户群在使用 WEB(ASP,PHP,...) 256万源代码下载- www.pudn.com...
  9. (浪漫七夕版)听七夕浪漫故事,玩酷炫浏览器
  10. spring task定时任务(个人笔记,非教程)