AOP实现 方法执行时间监控

目标:webapp工程,监控所有service方法执行时间。
工程:Spring+SpringMVC+MyBatis

AOP使用AspectJ 注解方式。关于Spring AOP总结阅读:https://www.jianshu.com/p/41632f76dd62

具体实现步骤

1、加入依赖

        <!-- aspectj 方式的AOP,需要加入下面三个包 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.9</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.8.9</version>
        </dependency>

2、开启AOP
在Spring配置文件中开启注解AOP

    <aop:aspectj-autoproxy proxy-target-class="true"/>

3、实现切面Aspect

package com.ljheee.tk.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.logging.Level;
import java.util.logging.Logger;

@Aspect
@Component
public class MonitorAop {

    private static final Logger logger = Logger.getLogger("MonitorAop");

    /**
     * 环绕通知
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("within(com.ljheee.tk.service.*) && execution(public * *(..))")
    public Object monitor(ProceedingJoinPoint pjp) throws Throwable {//ProceedingJoinPoint:用于环绕通知
        String targetClassName = pjp.getTarget().getClass().getSimpleName();
        String methodName = pjp.getSignature().getName();
        String methodFullName = targetClassName + "." + methodName;

        Object[] args = pjp.getArgs();// 方法参数

        Object result = null;
        try {
            logger.setLevel(Level.INFO);
            logger.info("methodFullName=" + methodFullName);
            long begin = System.currentTimeMillis();
            result = pjp.proceed();

            long delay = System.currentTimeMillis() - begin;
            logger.info("delay=" + delay);

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            logger.info("methodFullName=" + methodFullName + ",result=" + result);
        }

        return result;
    }
}



完整工程:https://github.com/ljheee/my-monitor-aop

代码性能测试

还可以利用PerformanceMonitorInterceptor/JamonPerformanceMonitorInterceptor来协助应用性能优化, 这是spring自带的。

但是它俩只是打印了是大量的原始数据日志,不幸的是这些东西对我们几乎没用,每一个方法调用都会有记录,而且缺乏一些其他信息。所以,除非你打算写一些日志分析程序、或者使用第三方软件,否则的话,我想你应该在日志被记录前做出一些处理。
https://my.oschina.net/jack230230/blog/65987

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,081评论 25 709
  • 1.引用类型有对象、数组、函数、正则,非引用类型有数值、布尔值、null、undefined 5.过滤如下数组,只...
    机智的大口袋阅读 199评论 0 0
  • 票之缘 有时候想偷偷的许个愿 缘分可不可以在我身上降临 让我和某人悄悄的靠近 让我不知不觉 到时候会说原...
    Z_erde阅读 136评论 0 2
  • 作者:大前研一 向麦肯锡学逻辑思考 大多数人在面对问题时,并没有认真地思考,而是单纯把“一时的想法”称之为解决之策...
    挖泥巴阅读 3,387评论 0 53