现在的位置: 首页 > 综合 > 正文

Spring AOP 详解

2013年04月28日 ⁄ 综合 ⁄ 共 5589字 ⁄ 字号 评论关闭

      此前对于AOP的使用仅限于声明式事务,除此之外在实际开发中也没有遇到过与之相关的问题。最近项目中遇到了以下几点需求,仔细思考之后,觉得采用AOP 来解决。一方面是为了以更加灵活的方式来解决问题,另一方面是借此机会深入学习Spring AOP相关的内容。本文是权当本人的自己AOP学习笔记,以下需求不用AOP肯定也能解决,至于是否牵强附会,仁者见仁智者见智。

  1. 对部分函数的调用进行日志记录,用于观察特定问题在运行过程中的函数调用情况
  2. 监控部分重要函数,若抛出指定的异常,需要以短信或邮件方式通知相关人员
  3. 金控部分重要函数的执行时间

    事实上,以上需求没有AOP也能搞定,只是在实现过程中比较郁闷摆了。

  1. 需要打印日志的函数分散在各个包中,只能找到所有的函数体,手动添加日志。然而这些日志都是临时的,待问题解决之后应该需要清除打印日志的代码,只能再次手动清除^_^!
  2. 类 似1的情况,需要捕获异常的地方太多,如果手动添加时想到很可能明天又要手动清除,只能再汗。OK,该需求相对比较固定,属于长期监控的范畴,并不需求临 时添加后再清除。然而,客户某天要求,把其中20%的异常改为短信提醒,剩下的80%改用邮件提醒。改之,两天后,客户抱怨短信太多,全部改成邮件提 醒...
  3. 该需求通常用于监控某些函数的执行时间,用以判断系统执行慢的瓶颈所在。瓶颈被解决之后,烦恼同情况1


    终于下定决心,采用AOP来解决!代码如下:

 

    切面类TestAspect

Java代码  收藏代码
  1. package com.spring.aop;  
  2. /** 
  3.  * 切面 
  4.  * 
  5.  */  
  6. public class TestAspect {  
  7.   
  8.     public void doAfter(JoinPoint jp) {  
  9.         System.out.println("log Ending method: "  
  10.                 + jp.getTarget().getClass().getName() + "."  
  11.                 + jp.getSignature().getName());  
  12.     }  
  13.   
  14.     public Object doAround(ProceedingJoinPoint pjp) throws Throwable {  
  15.         long time = System.currentTimeMillis();  
  16.         Object retVal = pjp.proceed();  
  17.         time = System.currentTimeMillis() - time;  
  18.         System.out.println("process time: " + time + " ms");  
  19.         return retVal;  
  20.     }  
  21.   
  22.     public void doBefore(JoinPoint jp) {  
  23.         System.out.println("log Begining method: "  
  24.                 + jp.getTarget().getClass().getName() + "."  
  25.                 + jp.getSignature().getName());  
  26.     }  
  27.   
  28.     public void doThrowing(JoinPoint jp, Throwable ex) {  
  29.         System.out.println("method " + jp.getTarget().getClass().getName()  
  30.                 + "." + jp.getSignature().getName() + " throw exception");  
  31.         System.out.println(ex.getMessage());  
  32.     }  
  33.   
  34.     private void sendEx(String ex) {  
  35.         //TODO 发送短信或邮件提醒  
  36.     }  
  37. }   

 

 

Java代码  收藏代码
  1. package com.spring.service;  
  2. /** 
  3.  * 接口A 
  4.  */  
  5. public interface AService {  
  6.       
  7.     public void fooA(String _msg);  
  8.   
  9.     public void barA();  
  10. }  

 

Java代码  收藏代码
  1. package com.spring.service;  
  2. /** 
  3.  *接口A的实现类 
  4.  */  
  5. public class AServiceImpl implements AService {  
  6.   
  7.     public void barA() {  
  8.         System.out.println("AServiceImpl.barA()");  
  9.     }  
  10.   
  11.     public void fooA(String _msg) {  
  12.         System.out.println("AServiceImpl.fooA(msg:"+_msg+")");  
  13.     }  
  14. }  

 

 

Java代码  收藏代码
  1. package com.spring.service;  
  2.   
  3. /** 
  4.  *   Service类B 
  5.  */  
  6. public class BServiceImpl {  
  7.   
  8.     public void barB(String _msg, int _type) {  
  9.         System.out.println("BServiceImpl.barB(msg:"+_msg+" type:"+_type+")");  
  10.         if(_type == 1)  
  11.             throw new IllegalArgumentException("测试异常");  
  12.     }  
  13.   
  14.     public void fooB() {  
  15.         System.out.println("BServiceImpl.fooB()");  
  16.     }  
  17.   
  18. }  

 

    ApplicationContext

Java代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="  
  6.             http://www.springframework.org/schema/beans  
  7.             http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8.             http://www.springframework.org/schema/aop  
  9.             http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"  
  10.     default-autowire="autodetect">  
  11.     <aop:config>  
  12.         <aop:aspect id="TestAspect" ref="aspectBean">  
  13.             <!--配置com.spring.service包下所有类或接口的所有方法-->  
  14.             <aop:pointcut id="businessService"  
  15.                 expression="execution(* com.spring.service.*.*(..))" />  
  16.             <aop:before pointcut-ref="businessService" method="doBefore"/>  
  17.             <aop:after pointcut-ref="businessService" method="doAfter"/>  
  18.             <aop:around pointcut-ref="businessService" method="doAround"/>  
  19.             <aop:after-throwing pointcut-ref="businessService" method="doThrowing" throwing="ex"/>  
  20.         </aop:aspect>  
  21.     </aop:config>  
  22.       
  23.     <bean id="aspectBean" class="com.spring.aop.TestAspect" />  
  24.     <bean id="aService" class="com.spring.service.AServiceImpl"></bean>  
  25.     <bean id="bService" class="com.spring.service.BServiceImpl"></bean>  
  26.   
  27. </beans>  

 

    测试类AOPTest

Java代码  收藏代码
  1. public class AOPTest extends AbstractDependencyInjectionSpringContextTests {  
  2.       
  3.     private AService aService;  
  4.       
  5.     private BServiceImpl bService;  
  6.       
  7.     protected String[] getConfigLocations() {  
  8.         String[] configs = new String[] { "/applicationContext.xml"};  
  9.         return configs;  
  10.     }  
  11.       
  12.       
  13.     /** 
  14.      * 测试正常调用 
  15.      */  
  16.     public void testCall()  
  17.     {  
  18.         System.out.println("SpringTest JUnit test");  
  19.         aService.fooA("JUnit test fooA");  
  20.         aService.barA();  
  21.         bService.fooB();  
  22.         bService.barB("JUnit test barB",0);  
  23.     }  
  24.       
  25.     /** 
  26.      * 测试After-Throwing 
  27.      */  
  28.     public void testThrow()  
  29.     {  
  30.         try {  
  31.             bService.barB("JUnit call barB",1);  
  32.         } catch (IllegalArgumentException e) {  
  33.               
  34.         }  
  35.     }  
  36.       
  37.     public void setAService(AService service) {  
  38.         aService = service;  
  39.     }  
  40.       
  41.     public void setBService(BServiceImpl service) {  
  42.         bService = service;  
  43.     }  
  44. }  

 

    运行结果如下:

Java代码  收藏代码
  1. log Begining method: com.spring.service.AServiceImpl.fooA  
  2. AServiceImpl.fooA(msg:JUnit test fooA)  
  3. log Ending method: com.spring.service.AServiceImpl.fooA  
  4. process time: 0 ms  
  5. log Begining method: com.spring.service.AServiceImpl.barA  
  6. AServiceImpl.barA()  
  7. log Ending method: com.spring.service.AServiceImpl.barA  
  8. process time: 0 ms  
  9. log Begining method: com.spring.service.BServiceImpl.fooB  
  10. BServiceImpl.fooB()  
  11. log Ending method: com.spring.service.BServiceImpl.fooB  
  12. process time: 0 ms  

抱歉!评论已关闭.