`
calatustela
  • 浏览: 141981 次
  • 性别: Icon_minigender_1
  • 来自: 江苏●南通
社区版块
存档分类
最新评论

SpringAop在项目中的一些巧妙使用(一)---方法执行时间记录

阅读更多

转载自:http://743389831.iteye.com/blog/1755241

 

     AOP的概念大家应该都知道吧,Aspect Oriented Programming的缩写,意为:面向切面编程(也叫面向方面),可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术。

     我们这样理解,AOP就是一个监控者,它在外面看着我们程序运行,同时也可以定一些规则,决定程序运行不运行,也可以在一个方法运行前进行处理,也可以在一个方法后进行一些逻辑处理。

    以上是我对AOP一些简单的理解,我们知道他可以在方法前进行处理,方法后进行处理,那我们可以做的时候就很多了,例如我们可以监控一些方法运行的时间,例如我们项目中使用它监控了sql语句的执行时间。请看代码。

Java代码  收藏代码
  1. package net.zoneland.test.common.util.aop;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.HashSet;  
  5. import java.util.Map;  
  6.   
  7. import org.aopalliance.intercept.MethodInterceptor;  
  8. import org.aopalliance.intercept.MethodInvocation;  
  9. import org.apache.commons.lang.time.StopWatch;  
  10. import org.apache.log4j.Logger;  
  11.   
  12. /** 
  13.  *用来监控方法的执行时间-- 对应配置文件是spring-method-aop.xml 
  14.  * @author wangyong 
  15.  * @version $Id: MethodTimeAdvice.java, v 0.1 2012-9-18 下午4:30:32 wangyong Exp $ 
  16.  */  
  17. public class MethodTimeAdvice implements MethodInterceptor {  
  18.   
  19.     private final static Logger logger = Logger.getLogger("DAL-MONITOR");  
  20.   
  21.     /** 
  22.      * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation) 
  23.      */  
  24.     public Object invoke(MethodInvocation invocation) throws Throwable {  
  25.         //用 commons-lang 提供的 StopWatch 计时,Spring 也提供了一个 StopWatch  
  26.         StopWatch clock = new StopWatch();  
  27.         clock.start(); //计时开始  
  28.         Object result = null;  
  29.         //监控的类名  
  30.         String className = invocation.getMethod().getDeclaringClass().getSimpleName();  
  31.         //监控的方法名  
  32.         String methodName = className + "." + invocation.getMethod().getName();  
  33.         try {  
  34.             //这个是我们监控的bean的执行并返回结果  
  35.             result = invocation.proceed();  
  36.         } catch (Throwable e) {  
  37.             //监控的参数  
  38.             Object[] objs = invocation.getArguments();  
  39.             logger.error("数据库执行异常,方法名:" + methodName + "参数:" + getString(objs), e);  
  40.             throw e;  
  41.         }  
  42.         clock.stop(); //计时结束  
  43.         if (logger.isInfoEnabled()) {  
  44.             logger.info("执行时间:" + clock.getTime() + " ms [" + methodName + "]");  
  45.         }  
  46.         return result;  
  47.     }  
  48.   
  49.     /** 
  50.      * 这个类主要是用于输出方法的参数 
  51.      *  
  52.      * @param objs 
  53.      * @return 
  54.      */  
  55.     @SuppressWarnings("unchecked")  
  56.     public String getString(Object[] objs) {  
  57.         StringBuffer stringBuffer = new StringBuffer();  
  58.         for (int i = 0, len = objs.length; i < len; i++) {  
  59.             if (objs[i] instanceof String) {  
  60.                 stringBuffer.append("String类型:" + objs[i].toString());  
  61.             } else if (objs[i] instanceof Map) {  
  62.                 HashMap<String, Object> hashMap = (HashMap<String, Object>) objs[i];  
  63.                 HashMap<String, Object> map = hashMap;  
  64.                 HashSet<String> set = (HashSet<String>) map.keySet();  
  65.                 stringBuffer.append("Map类型");  
  66.                 for (String str : set) {  
  67.                     stringBuffer.append(str + "=" + map.get(str));  
  68.                 }  
  69.             } else if (objs[i] instanceof Integer) {  
  70.                 stringBuffer.append("整数类型:");  
  71.                 stringBuffer.append(objs[i].toString());  
  72.             } else {  
  73.                 stringBuffer.append(objs[i].toString());  
  74.             }  
  75.         }  
  76.         return stringBuffer.toString();  
  77.     }  
  78. }  

 相对应的配置文件

Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"  
  3.      "http://www.springframework.org/dtd/spring-beans.dtd">  
  4. <beans>  
  5.   
  6.     <bean id="methodTimeAdvice" class="net.zoneland.ums.common.dal.aop.MethodTimeAdvice" />  
  7.   
  8.       
  9.     <!-- 根据 Bean 的名字自动实现代理拦截 -->  
  10.     <bean  
  11.         class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  12.         <property name="interceptorNames">  
  13.             <!-- 我们的监控方法 -->>  
  14.             <list>  
  15.                 <value>methodTimeAdvice</value>  
  16.             </list>  
  17.         </property>  
  18.         <property name="beanNames">  
  19.             <list>  
  20.                 <!-- 添加到其中的 Bean 自动就被代理拦截了 -->  
  21.                 <value>*Mapper</value>  
  22.             </list>  
  23.         </property>  
  24.     </bean>  
  25. </beans>  

 这样配置之后我们就可以监控以Mapper结尾的方法了。同时我们可以监控其他的各种方法,只要我们进行相应的配置。

使用这种方式记录日志,下回分享。

分享到:
评论

相关推荐

    spring-aop.jar各个版本

    spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...

    开发工具 spring-aop-4.3.6.RELEASE

    开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...

    spring-aop-5.0.8.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.0.8.RELEASE.jar; 赠送原API文档:spring-aop-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.8.RELEASE.pom;...

    spring-aop-5.2.0.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...

    spring-aop-jar

    aopalliance.jar、spring-aop-4.1.6.RELEASE.jar、spring-aspects-4.1.6.RELEASE.jar

    spring-aop-5.3.10-API文档-中文版.zip

    赠送jar包:spring-aop-5.3.10.jar; 赠送原API文档:spring-aop-5.3.10-javadoc.jar; 赠送源代码:spring-aop-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.10.pom; 包含翻译后的API文档:spring...

    spring-aop-5.0.10.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....

    Spring5 框架 ---- AOP ---- 代码

    Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 ...

    Java利用spring aop进行监测方法执行耗时

    使用 Spring AOP 进行方法耗时监测的好处有以下几点: 1. 代码实现简单,易于维护:使用 Spring AOP 可以将耗时监测的逻辑与业务逻辑进行解耦,避免业务逻辑代码的冗余和代码维护难度的提高。 2. 安全性高:使用 ...

    spring-aop-5.1.3.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.1.3.RELEASE.jar; 赠送原API文档:spring-aop-5.1.3.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.1.3.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.1.3.RELEASE.pom;...

    spring-aop-4.2.2.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-4.2.2.RELEASE.jar; 赠送原API文档:spring-aop-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-aop-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-4.2.2.RELEASE.pom;...

    spring-aop-4.3.20.RELEASE-API文档-中英对照版.zip

    赠送jar包:spring-aop-4.3.20.RELEASE.jar 赠送原API文档:spring-aop-4.3.20.RELEASE-javadoc.jar 赠送源代码:spring-aop-4.3.20.RELEASE-sources.jar 包含翻译后的API文档:spring-aop-4.3.20.RELEASE-...

    spring-aop-5.3.12-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.3.12.jar; 赠送原API文档:spring-aop-5.3.12-javadoc.jar; 赠送源代码:spring-aop-5.3.12-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.12.pom; 包含翻译后的API文档:spring...

    AOP流程源码分析-SpringAOP中定义的类图

    AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-...

    spring-aop-2.0.8.jar

    spring-aop-2.0.8.jar

    spring-aop-5.2.15.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.2.15.RELEASE.jar; 赠送原API文档:spring-aop-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.15.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.15.RELEASE....

    spring aop测试项目

    spring aop测试项目

    spring-aop-5.3.15-API文档-中英对照版.zip

    赠送jar包:spring-aop-5.3.15.jar; 赠送原API文档:spring-aop-5.3.15-javadoc.jar; 赠送源代码:spring-aop-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.15.pom; 包含翻译后的API文档:spring...

    spring-aop-5.3.15-API文档-中文版.zip

    赠送jar包:spring-aop-5.3.15.jar; 赠送原API文档:spring-aop-5.3.15-javadoc.jar; 赠送源代码:spring-aop-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.15.pom; 包含翻译后的API文档:spring...

    spring-aop-4.3.20.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-4.3.20.RELEASE.jar; 赠送原API文档:spring-aop-4.3.20.RELEASE-javadoc.jar; 赠送源代码:spring-aop-4.3.20.RELEASE-sources.jar; 包含翻译后的API文档:spring-aop-4.3.20.RELEASE-...

Global site tag (gtag.js) - Google Analytics