package necs.omms.common.aop;

import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Component
@Aspect
@CommonsLog
public class PerformanceMonitor {
private static Log log = LogFactory.getLog(PerformanceMonitor.class); /**
* A join point is in the controller layer if the method is
* modified by public and defined in a type in the
* com.shawn.service package or any sub-package under that
* and modified by public.execution(* com.baobaotao..*(..))l
*/
@Pointcut("execution( * necs.omms.controller..*(..))")
private void controllerLayer() {
} /**
* Monitor the elapsed time of method on controller layer, in
* order to detect performance problems as soon as possible.
* If elapsed time > 1 s, log it as an error. Otherwise, log it
* as an info.
*/
@Around("controllerLayer()")
public Object monitorElapsedTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
// Timing the method in controller layer
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Object result = proceedingJoinPoint.proceed();
stopWatch.stop(); // Log the elapsed time
double elapsedTime = stopWatch.getTime() / 1000;
Signature signature = proceedingJoinPoint.getSignature();
String infoString = "[" + signature.toShortString() + "][Elapsed time: " + elapsedTime + " s]";
if (elapsedTime > 1) {
log.error(infoString + "[Note that it's time consuming!]");
} else {
log.info(infoString);
} // Return the result
return result;
} @Before(value = "controllerLayer()")
public void doBefore(){
System.out.println("-------------@BEFORE------------");
} }

aop计算方法耗时的更多相关文章

  1. AOP计算方法执行时长

    AOP计算方法执行时长 依赖引入 <dependency> <groupId>org.springframework.boot</groupId> <arti ...

  2. springMVC Aspect AOP 接口耗时统计

    在接口开发中,我们通常需要统计接口耗时,为后续接口性能做统计.在springMVC中可以用它的aop来记录日志. 1.在spring配置文件中开启AOP <!--*************** ...

  3. 利用spring AOP 和注解实现方法中查cache-我们到底能走多远系列(46)

    主题:这份代码是开发中常见的代码,查询数据库某个主表的数据,为了提高性能,做一次缓存,每次调用时先拿缓存数据,有则直接返回,没有才向数据库查数据,降低数据库压力. public Merchant lo ...

  4. Spring Aop 应用实例与设计浅析

    0.代码概述 代码说明:第一章中的代码为了突出模块化拆分的必要性,所以db采用了真实操作.下面代码中dao层使用了打印日志模拟插入db的方法,方便所有人运行demo. 1.项目代码地址:https:/ ...

  5. 实现自己的BeanFactory、AOP以及声明式事务

    实现自己的BeanFactory                                                                   在使用spring时,我们很少用& ...

  6. Android Activity启动耗时统计方案

    作者:林基宗 Activity的启动速度是很多开发者关心的问题,当页面跳转耗时过长时,App就会给人一种非常笨重的感觉.在遇到某个页面启动过慢的时候,开发的第一直觉一般是onCreate执行速度太慢了 ...

  7. JAVA提高八:动态代理技术

    对于动态代理,学过AOP的应该都不会陌生,因为代理是实现AOP功能的核心和关键技术.那么今天我们将开始动态代理的学习: 一.引出动态代理 生活中代理应该是很常见的,比如你可以通过代理商去买电脑,也可以 ...

  8. TOP100summit:【分享实录】爆炸式增长的斗鱼架构平台的演进

    本篇文章内容来自2016年TOP100summit斗鱼数据平台部总监吴瑞城的案例分享. 编辑:Cynthia 吴瑞诚:斗鱼数据平台部总监 曾先后就职于淘宝.一号店. 从0到1搭建公司大数据平台.平台规 ...

  9. 【springboot】给你一份Spring Boot知识清单

    目录: 一.抛砖引玉:探索Spring IoC容器 1.1.Spring IoC容器 1.2.Spring容器扩展机制 二.夯实基础:JavaConfig与常见Annotation 2.1.JavaC ...

随机推荐

  1. Activity Process Task Application 专题讲解

    Activity Process Task Application 专题讲解 Activity.和进程 为了阅读方便,将文档转成pdf http://files.cnblogs.com/franksu ...

  2. 通用线程:POSIX 线程详解,第 3 部分

    通用线程:POSIX 线程详解,第 3 部分 使用条件变量提高效率 Daniel Robbins, 总裁兼 CEO, Gentoo Technologies, Inc. 简介: 本文是 POSIX 线 ...

  3. Java得到当前系统时间,精确到毫秒的几种方法

    import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class Ma ...

  4. map/reduce类简单介绍

    在Hadoop的mapper类中,有4个主要的函数,分别是:setup,clearup,map,run.代码如下: protected void setup(Context context) thro ...

  5. VS2013 快捷方式

    1.查找空行:  使用正则表达式 ^\s\S*$\n

  6. js 预解析

    前言 JavaScript是解释型语言是毋庸置疑的,但它是不是仅在运行时自上往下一句一句地解析的呢? 事实上或某种现象证明并不是这样的,通过<JavaScript权威指南>及网上相关资料了 ...

  7. vba打开excel文件遍历sheet的名字和指定单元格的值

    今天项目上有个应用,获取指定Excel文件下的所有sheet的名称以及当前sheet中指定单元格的值,并把他们写到固定的sheet中去,看了下,文件比较多,而且每个文件sheet的个数比较多,也不一样 ...

  8. 解决 Flask-sqlalchemy 中文乱码

    当在使用 flask-sqlalchemy 操作 mysql 数据库时中文乱码或者直接报错. 解决方法如下: from flask_sqlalchemy import SQLAlchemy db = ...

  9. numpy之初识ndarray

    Numpy ndarray numpy的最重要特点就是其N维数组对象(ndarray). ndarray的可以对整块数据执行数学运算,语法与标量元素的元素的运算一致. 如: import numpy ...

  10. laravel 中config的使用

    在laravel的config中添加配置文件(比如:alipay.php)文件内容为return数组的形式 在方法中使用config()函数获取数据 $config = config('alipay. ...