热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SpringAOP记录日志

  去年的时候由于业务量快速上升,导致我们提供的很多接口或者第三方的接口都出现性能问题,请求处理时间都特别长,为了能够快速检查出哪些接口或者业务方法响应时间特别长,PM让我

   去年的时候由于业务量快速上升,导致我们提供的很多接口或者第三方的接口都出现性能问题,请求处理时间都特别长,为了能够快速检查出哪些接口或者业务方法响应时间特别长,PM让我们记录下每个接口或方法的处理时长。就一句MMP,要在每个方法之前之后都加上当前时间然后在相减?特么的想想都要死人了,这特么没法工作了。但是在程序员的思维里是:重复的东西我们要让程序去做,我们负责实现这个程序。

好了,废话不说。

框架:Maven+Spring

一、通过XML配置实现AOP 记录方法执行时间

   1、引入jar

     
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.8.9version>
dependency>

<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>

<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
<scope>providedscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>

<scope>testscope>
dependency>

 

    

   2、编写通知类

package com.ssm.interf;
import
org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;
public class MethodTimeAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
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 ) {
simpleParams[i] = params[i].getSimpleName();
}
System.out.println(
"Takes:" + clock.getTotalTimeMillis() + " ms [" + invocation.getThis().getClass().getName() + "."
+ invocation.getMethod().getName() + "(" + StringUtils.arrayToDelimitedString(simpleParams, ",") + ")]");
return result;
}
}

 

 StopWatch 是Spring 提供的一个计时类。

  3、spring的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:aop="http://www.springframework.org/schema/aop"
xmlns:tx
="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"
>
<context:component-scan base-package="com.ssm" />

<aop:config>
<aop:pointcut id="runTimePc" expression="execution(* com.ssm.service.impl.*.*(..))" />

<aop:advisor advice-ref="methodTimeAdvice" pointcut-ref="runTimePc" />

aop:config>

<bean id="methodTimeAdvice" class="com.ssm.interf.MethodTimeAdvice" />
beans>

 

expression="execution(* com.ssm.service.impl.*.*(..))" 代表切入点,意思是com.ssm.service.impl包下的所有方法都会被切入 时间记录
  4、测试的类

package com.ssm.service.impl;
import org.springframework.stereotype.Service;
import com.ssm.service.AopLogService;
@Service(
"aopLogService")
public class AopLogServiceImpl implements AopLogService {
public void operation1() {
System.out.println(
"运行方法:operation1");
try {
Thread.sleep(
1000);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void operation2() {
System.out.println(
"运行方法:operation2");
try {
Thread.sleep(
2000);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void operation3() {
System.out.println(
"运行方法:operation3");
try {
Thread.sleep(
3000);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

 

5、junit 运行测试

 

package org.logRumTime;
import org.BaseTest;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.ssm.service.AopLogService;
public class RumTimeTest extends BaseTest {
@Autowired
private AopLogService aopLogService;
@Test
public void testOperation() {
aopLogService.operation1();
aopLogService.operation2();
aopLogService.operation3();
}
public AopLogService getAopLogService() {
return aopLogService;
}
public void setAopLogService(AopLogService aopLogService) {
this.aopLogService = aopLogService;
}
}

BaseTest 类代码

package org;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.
class)//使用junit4进行测试
@ContextConfiguration(locatiOns= {"classpath:app-context-core.xml"})
public class BaseTest {
}

 

运行结果:

 

 


 



推荐阅读
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文介绍了使用PHP实现断点续传乱序合并文件的方法和源码。由于网络原因,文件需要分割成多个部分发送,因此无法按顺序接收。文章中提供了merge2.php的源码,通过使用shuffle函数打乱文件读取顺序,实现了乱序合并文件的功能。同时,还介绍了filesize、glob、unlink、fopen等相关函数的使用。阅读本文可以了解如何使用PHP实现断点续传乱序合并文件的具体步骤。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • 标题: ... [详细]
  • 本文介绍了在多平台下进行条件编译的必要性,以及具体的实现方法。通过示例代码展示了如何使用条件编译来实现不同平台的功能。最后总结了只要接口相同,不同平台下的编译运行结果也会相同。 ... [详细]
author-avatar
山野木每子
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有