在项目中,我们往往需要记录数据库操作的时间,根据操作时间的不同,分别记录不同等级的日志。

  首先我们可以写一个类实现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记录数据库操作的执行时间的更多相关文章

  1. 记录数据库操作记录的DDL触发器

    我们在项目中经常会对数据做一些操作,比如增加一个字段,修改一个存储过程,删除表等等操作,很有必要记录这些操作,以便以后出了问题,方便找到元凶.接下来介绍一个DDL触发器在实际环境中的使用,这个DDL触 ...

  2. aop 记录用户操作(一)

    转载: http://www.cnblogs.com/guokai870510826/p/5981015.html 使用标签来设置需要的记录 实例:@ISystemLog() @Controller ...

  3. 拦截并记录数据库操作-Logging and Intercepting Database Operations

    原文:http://msdn.microsoft.com/zh-cn/data/dn469464 Logging and Intercepting Database Operations Starti ...

  4. Spring学习总结(16)——Spring AOP实现执行数据库操作前根据业务来动态切换数据源

    深刻讨论为什么要读写分离? 为了服务器承载更多的用户?提升了网站的响应速度?分摊数据库服务器的压力?就是为了双机热备又不想浪费备份服务器?上面这些回答,我认为都不是错误的,但也都不是完全正确的.「读写 ...

  5. mysql--->mysql查看数据库操作记录

    mysql查看数据库操作记录 MySQL的查询日志记录了所有MySQL数据库请求的信息.无论这些请求是否得到了正确的执行.默认文件名为hostname.log.默认情况下MySQL查询日志是关闭的.生 ...

  6. 百万年薪python之路 -- MySQL数据库之 MySQL行(记录)的操作(二) -- 多表查询

    MySQL行(记录)的操作(二) -- 多表查询 数据的准备 #建表 create table department( id int, name varchar(20) ); create table ...

  7. 百万年薪python之路 -- MySQL数据库之 MySQL行(记录)的操作(一)

    MySQL的行(记录)的操作(一) 1. 增(insert) insert into 表名 value((字段1,字段2...); # 只能增加一行记录 insert into 表名 values(字 ...

  8. SpringSecurity权限管理系统实战—八、AOP 记录用户、异常日志

    目录 SpringSecurity权限管理系统实战-一.项目简介和开发环境准备 SpringSecurity权限管理系统实战-二.日志.接口文档等实现 SpringSecurity权限管理系统实战-三 ...

  9. EntityFramework的多种记录日志方式,记录错误并分析执行时间过长原因(系列4)

    前言 Entity Framework 延伸系列目录 今天我们来聊聊EF的日志记录. 一个好的数据库操作记录不仅仅可以帮你记录用户的操作, 更应该可以帮助你获得效率低下的语句来帮你提高运行效率 废话不 ...

随机推荐

  1. cocos2dx使用TiledMap创建斜45度地图场景

    做游戏,场景是一个很重要的部分,如果缺少这一步,很难做出好的游戏,对于cocos2dx来说,有很多2D的地图编辑器可以用,效果都还可以,其中Tiled是支持的比较好的,它支持Tiled编辑出来的几种模 ...

  2. java懒汉式单例遇到多线程

    单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 在计算机系统中,线程池.缓存.日志对象.对话框.打印机.显卡的驱动程序对象常被设计成单例.这些应用都或多或少具有资源管理器的功 ...

  3. Android - Facebook KeyHash 設定

    转自:http://www.dotblogs.com.tw/newmonkey48/archive/2014/04/17/144779.aspx App要使用Facebook 分享時,設要在Faceb ...

  4. 查看sid

    查看用户sid: whoami /user 查看系统sid: 使用PSTools工具中的 psgetsid.exe命令查看

  5. ReactNative学习实践--Navigator实践

    离上次写RN笔记有一段时间了,期间参与了一个新项目,只在最近的空余时间继续学习实践,因此进度比较缓慢,不过这并不代表没有新进展,其实这个小东西离上次发文时已经有了相当大的变化了,其中影响最大的变化就是 ...

  6. Codeforces Gym 100015G Guessing Game 差分约束

    Guessing Game 题目连接: http://codeforces.com/gym/100015/attachments Description Jaehyun has two lists o ...

  7. hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)

    pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/ ...

  8. [Express] Level 3: Massaging User Data

    Flexible Routes Our current route only works when the city name argument matches exactly the propert ...

  9. JAVA正则表达式语法大全

    [正则表达式]文本框输入内容控制 整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$ 只能输入数字:"^[0-9]*$". 只能输入n位的数字:"^\d{n ...

  10. oc-15-self

    // // Person.m // OC基础第三天 // // Created by apple on 15/10/17. // // #import "Person.h" @im ...