使用aop记录数据库操作的执行时间
在项目中,我们往往需要记录数据库操作的时间,根据操作时间的不同,分别记录不同等级的日志。
首先我们可以写一个类实现MethodInterceptor接口:
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; /**
* 自定义 AOP 处理时间类
*/
public class TimeHandler implements MethodInterceptor {
/**
* log
*/
private final static Log log = LogFactory.getLog(TimeHandler.class);
/**
* 对于执行时间超过指定毫秒,日志级别为error
* 单位:毫秒
* 默认值:200
*/
private int error = 200;
/**
* 对于执行时间超过指定毫秒,日志级别为warn
* 单位:毫秒
* 默认值:100
*/
private int warn = 100;
/**
* 对于执行时间超过指定毫秒,日志级别为info
* 单位:毫秒
* 默认值:50
*/
private int info = 50; /**
* Method invoke ...
*
* @param methodInvocation of type MethodInvocation
* @return Object
* @throws Throwable when
*/
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
long procTime = System.currentTimeMillis();
try {
return methodInvocation.proceed();
}
finally {
procTime = System.currentTimeMillis() - procTime;
String msg = "Process method " + methodInvocation.getMethod().getName() + " successful! Total time: " + procTime + " milliseconds!";
if (procTime > error) {
if (log.isErrorEnabled()) log.error(msg);
} else if (procTime > warn) {
if (log.isWarnEnabled()) log.warn(msg);
} else if (procTime > info) {
if (log.isInfoEnabled()) log.info(msg);
} else {
if (log.isDebugEnabled()) log.debug(msg);
}
}
} /**
* Method setError sets the error of this TimeHandler object.
*
* @param error the error of this TimeHandler object.
*/
public void setError(int error) {
this.error = error;
} /**
* Method setWarn sets the warn of this TimeHandler object.
*
* @param warn the warn of this TimeHandler object.
*/
public void setWarn(int warn) {
this.warn = warn;
} /**
* Method setInfo sets the info of this TimeHandler object.
*
* @param info the info of this TimeHandler object.
*/
public void setInfo(int info) {
this.info = info;
}
}
然后我们可以在Spring中配置
<!-- Dao方法处理时间 -->
<bean id="daoTimeHandler" class="TimeHandler"/> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>*Dao</value>
</property>
<property name="interceptorNames">
<list>
<value>daoTimeHandler</value>
</list>
</property>
</bean>
以上在运行时就可以在每个*Dao执行后记录该操作的使用时间。
使用aop记录数据库操作的执行时间的更多相关文章
- 记录数据库操作记录的DDL触发器
我们在项目中经常会对数据做一些操作,比如增加一个字段,修改一个存储过程,删除表等等操作,很有必要记录这些操作,以便以后出了问题,方便找到元凶.接下来介绍一个DDL触发器在实际环境中的使用,这个DDL触 ...
- aop 记录用户操作(一)
转载: http://www.cnblogs.com/guokai870510826/p/5981015.html 使用标签来设置需要的记录 实例:@ISystemLog() @Controller ...
- 拦截并记录数据库操作-Logging and Intercepting Database Operations
原文:http://msdn.microsoft.com/zh-cn/data/dn469464 Logging and Intercepting Database Operations Starti ...
- Spring学习总结(16)——Spring AOP实现执行数据库操作前根据业务来动态切换数据源
深刻讨论为什么要读写分离? 为了服务器承载更多的用户?提升了网站的响应速度?分摊数据库服务器的压力?就是为了双机热备又不想浪费备份服务器?上面这些回答,我认为都不是错误的,但也都不是完全正确的.「读写 ...
- mysql--->mysql查看数据库操作记录
mysql查看数据库操作记录 MySQL的查询日志记录了所有MySQL数据库请求的信息.无论这些请求是否得到了正确的执行.默认文件名为hostname.log.默认情况下MySQL查询日志是关闭的.生 ...
- 百万年薪python之路 -- MySQL数据库之 MySQL行(记录)的操作(二) -- 多表查询
MySQL行(记录)的操作(二) -- 多表查询 数据的准备 #建表 create table department( id int, name varchar(20) ); create table ...
- 百万年薪python之路 -- MySQL数据库之 MySQL行(记录)的操作(一)
MySQL的行(记录)的操作(一) 1. 增(insert) insert into 表名 value((字段1,字段2...); # 只能增加一行记录 insert into 表名 values(字 ...
- SpringSecurity权限管理系统实战—八、AOP 记录用户、异常日志
目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战-三 ...
- EntityFramework的多种记录日志方式,记录错误并分析执行时间过长原因(系列4)
前言 Entity Framework 延伸系列目录 今天我们来聊聊EF的日志记录. 一个好的数据库操作记录不仅仅可以帮你记录用户的操作, 更应该可以帮助你获得效率低下的语句来帮你提高运行效率 废话不 ...
随机推荐
- Firefox 设置技巧
在Firefox地址栏中输入“about:cache”并键入回车,接着将显示Firefox的内存缓冲设置与磁盘高速缓存设置.如果在页面上单击“List Cache Entries”链接,我们还可以查看 ...
- Spring MVC体系结构
[Spring MVC类图]<Spring实战>中:<Spring3.0就这么简单>中:[http://blog.csdn.net/gstormspire/article/de ...
- 窥探EasyMock(2)进阶使用篇
from:http://www.iteye.com/topic/310313 1. 生成 Mock 对象 如何创建一个需要严格遵守调用顺序的mock对象? SomeInterface mockObj ...
- jsp中使用动态数据进行mySQL数据库的两种操作方法
使用动态数据进行数据库内容的增删改查操作有两种方法: 在此定义数据库连接为conn 假设有表单进行数据输入并提交到处理页面一种是使用预编译格式: 其格式如下: String name = reques ...
- 鼠标指向GridView某列显示DIV浮动列表
需求: 当GRIDVIEW数据列过多,不方便全部显示在同一行或者一些子信息需要鼠标指向某关键列GRIDVIEW的时候显示其子信息. 设计:先把需要显示的浮动数据一次过抓取出来.而不是鼠标指向的时候才从 ...
- KVO机制
KVO,全称为Key-Value Observing,是iOS中的一种设计模式,用来监测对象的某些属性的实时变化情况并作出响应 首先,假设我们的目标是在一个UITableViewController内 ...
- 从零开始学android开发-adt-bundle-eclipse下的修改android app名称
eclipse中,打开项目根目录中的AndoirManifest.xml文件,找到如下内容 <application android:allowBackup="true" a ...
- Android 百度地图定位(手动+自动) 安卓开发教程
近由于项目需要,研究了下百度地图定位,他们提供的实例基本都是用监听器实现自动定位的.我想实现一种效果:当用户进入UI时,不定位,用户需要定位的时候,自己手动点击按钮,再去定位当前位置. 经过2天研究 ...
- [Angular 2] Move and Delete Angular 2 Components After Creation
After the original order is set, you can still leverage methods on the Angular 2 ViewContainer to re ...
- scala目录
1. 使用Scala(构建这样一个应用,它会取回一份列表,其中包括用户持有的股票的代码以及股份,并告知他们在当前日期为止的这些投资的总价.这包含了几件事:获取用户输入.读文件.解析数据.写文件.从We ...