• Spring Aop实例之AspectJ注解配置


    http://blog.csdn.net/xiaoxian8023/article/details/17285809

    上篇博文《Spring Aop实例之xml配置》中,讲解了xml配置方式,今天来说说AspectJ注解方式去配置spring aop。

           依旧采用的jdk代理,接口和实现类代码请参考上篇博文。主要是将Aspect类分享一下:

    [java] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. package com.tgb.aop;  
    2.   
    3. import org.aspectj.lang.JoinPoint;  
    4. import org.aspectj.lang.ProceedingJoinPoint;  
    5. import org.aspectj.lang.annotation.After;  
    6. import org.aspectj.lang.annotation.AfterReturning;  
    7. import org.aspectj.lang.annotation.AfterThrowing;  
    8. import org.aspectj.lang.annotation.Around;  
    9. import org.aspectj.lang.annotation.Aspect;  
    10. import org.aspectj.lang.annotation.Before;  
    11. import org.aspectj.lang.annotation.DeclareParents;  
    12. import org.aspectj.lang.annotation.Pointcut;  
    13.   
    14. /** 
    15.  * 测试after,before,around,throwing,returning Advice. 
    16.  * @author Admin 
    17.  * 
    18.  */  
    19. @Aspect  
    20. public class AspceJAdvice {  
    21.   
    22.     /** 
    23.      * Pointcut 
    24.      * 定义Pointcut,Pointcut的名称为aspectjMethod(),此方法没有返回值和参数 
    25.      * 该方法就是一个标识,不进行调用 
    26.      */  
    27.     @Pointcut("execution(* find*(..))")  
    28.     private void aspectjMethod(){};  
    29.       
    30.     /**  
    31.      * Before 
    32.      * 在核心业务执行前执行,不能阻止核心业务的调用。 
    33.      * @param joinPoint  
    34.      */    
    35.     @Before("aspectjMethod()")    
    36.     public void beforeAdvice(JoinPoint joinPoint) {    
    37.         System.out.println("-----beforeAdvice().invoke-----");  
    38.         System.out.println(" 此处意在执行核心业务逻辑前,做一些安全性的判断等等");  
    39.         System.out.println(" 可通过joinPoint来获取所需要的内容");  
    40.         System.out.println("-----End of beforeAdvice()------");  
    41.     }  
    42.       
    43.     /**  
    44.      * After  
    45.      * 核心业务逻辑退出后(包括正常执行结束和异常退出),执行此Advice 
    46.      * @param joinPoint 
    47.      */  
    48.     @After(value = "aspectjMethod()")    
    49.     public void afterAdvice(JoinPoint joinPoint) {    
    50.         System.out.println("-----afterAdvice().invoke-----");  
    51.         System.out.println(" 此处意在执行核心业务逻辑之后,做一些日志记录操作等等");  
    52.         System.out.println(" 可通过joinPoint来获取所需要的内容");  
    53.         System.out.println("-----End of afterAdvice()------");  
    54.     }    
    55.   
    56.     /**  
    57.      * Around  
    58.      * 手动控制调用核心业务逻辑,以及调用前和调用后的处理, 
    59.      *  
    60.      * 注意:当核心业务抛异常后,立即退出,转向AfterAdvice 
    61.      * 执行完AfterAdvice,再转到ThrowingAdvice 
    62.      * @param pjp 
    63.      * @return 
    64.      * @throws Throwable 
    65.      */   
    66.     @Around(value = "aspectjMethod()")    
    67.     public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {    
    68.         System.out.println("-----aroundAdvice().invoke-----");  
    69.         System.out.println(" 此处可以做类似于Before Advice的事情");  
    70.           
    71.         //调用核心逻辑  
    72.         Object retVal = pjp.proceed();  
    73.         System.out.println(" 此处可以做类似于After Advice的事情");  
    74.         System.out.println("-----End of aroundAdvice()------");  
    75.         return retVal;  
    76.     }    
    77.       
    78.     /**  
    79.      * AfterReturning  
    80.      * 核心业务逻辑调用正常退出后,不管是否有返回值,正常退出后,均执行此Advice 
    81.      * @param joinPoint 
    82.      */   
    83.     @AfterReturning(value = "aspectjMethod()", returning = "retVal")    
    84.     public void afterReturningAdvice(JoinPoint joinPoint, String retVal) {    
    85.         System.out.println("-----afterReturningAdvice().invoke-----");  
    86.         System.out.println("Return Value: " + retVal);   
    87.         System.out.println(" 此处可以对返回值做进一步处理");  
    88.         System.out.println(" 可通过joinPoint来获取所需要的内容");  
    89.         System.out.println("-----End of afterReturningAdvice()------");  
    90.     }  
    91.       
    92.     /** 
    93.      * 核心业务逻辑调用异常退出后,执行此Advice,处理错误信息 
    94.      *  
    95.      * 注意:执行顺序在Around Advice之后 
    96.      * @param joinPoint 
    97.      * @param ex 
    98.      */  
    99.     @AfterThrowing(value = "aspectjMethod()", throwing = "ex")    
    100.     public void afterThrowingAdvice(JoinPoint joinPoint, Exception ex) {    
    101.         System.out.println("-----afterThrowingAdvice().invoke-----");  
    102.         System.out.println(" 错误信息:"+ex.getMessage());  
    103.         System.out.println(" 此处意在执行核心业务逻辑出错时,捕获异常,并可做一些日志记录操作等等");  
    104.         System.out.println(" 可通过joinPoint来获取所需要的内容");  
    105.         System.out.println("-----End of afterThrowingAdvice()------");    
    106.     }    
    107. }  



           application-config.xml中,只需要配置业务逻辑bean和Aspect bean,并启用Aspect注解即可:

     
      1. <?xml version="1.0" encoding="UTF-8"?>  
      2.   
      3. <beans xmlns="http://www.springframework.org/schema/beans"  
      4.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      5.          xmlns:aop="http://www.springframework.org/schema/aop"  
      6.          xmlns:tx="http://www.springframework.org/schema/tx"  
      7.          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
      8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
      9.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
      10.              
      11.     <!-- 启用AspectJ对Annotation的支持 -->         
      12.     <aop:aspectj-autoproxy/>             
      13.       
      14.     <bean id="userManager" class="com.tgb.aop.UserManagerImpl"/>  
      15.     <bean id="aspcejHandler" class="com.tgb.aop.AspceJAdvice"/>  
      16.       
      17. </beans>  

    通过测试的发现AroundAdvice、BeforeAdvice、AfterAdvice、ReturningAdvice的执行顺序是根据注解的顺序而定的。但是有时候修改了顺序,结果却没有变化,可能是缓存的缘故。前几天我也遇到了这样的问题,不过今天再测试了一下,发现执行顺序又跟注解的顺序一致了。

           xml 和 Annotation 注解都可以作为配置项,对Spring AoP进行配置管理,那么它们各自都有什么优缺点呢?

      首先说说 xml 。目前 web 应用中几乎都使用 xml 作为配置项,例如我们常用的框架 Struts、Spring、Hibernate 等等都采用 xml 作为配置。xml 之所以这么流行,是因为它的很多优点是其它技术的配置所无法替代的:

      • xml 作为可扩展标记语言最大的优势在于开发者能够为软件量身定制适用的标记,使代码更加通俗易懂。
      • 利用 xml 配置能使软件更具扩展性。例如 Spring 将 class 间的依赖配置在 xml 中,最大限度地提升应用的可扩展性。
      • 具有成熟的验证机制确保程序正确性。利用 Schema 或 DTD 可以对 xml 的正确性进行验证,避免了非法的配置导致应用程序出错。
      • 修改配置而无需变动现有程序。

      虽然有如此多的好处,但毕竟没有什么万能的东西,xml 也有自身的缺点。

      • 需要解析工具或类库的支持。
      • 解析 xml 势必会影响应用程序性能,占用系统资源。
      • 配置文件过多导致管理变得困难。
      • 编译期无法对其配置项的正确性进行验证,或要查错只能在运行期。
      • IDE 无法验证配置项的正确性无能为力。
      • 查错变得困难。往往配置的一个手误导致莫名其妙的错误。
      • 开发人员不得不同时维护代码和配置文件,开发效率变得低下。
      • 配置项与代码间存在潜规则。改变了任何一方都有可能影响另外一方。

      让我们来看看 Annotation 的优点。

      • 保存在 class 文件中,降低维护成本。
      • 无需工具支持,无需解析。
      • 编译期即可验证正确性,查错变得容易。
      • 提升开发效率。

      同样 Annotation 也不是万能的,它也有很多缺点。

      • 若要对配置项进行修改,不得不修改 Java 文件,重新编译打包应用。
      • 配置项编码在 Java 文件中,可扩展性差。

      总结:没有一个事物是万能的,同样 xml 和 Java Annotation 都有各自的优缺点。通过以上对比,细心的读者可能已经发现它们的优缺点恰恰是互补的。xml 的强项是 Annotation 所不具备的,而 Annotation 的优势也是 xml 所欠缺的。这也正是时下流行的 xml + Annotation 配置的原因所在。平衡才是王道呀!

  • 相关阅读:
    VC++6.0 自定义按钮,无标题对话框的拖动方法
    完整的使用线程池的多线程C/S Socket类
    树形控件Tree Control
    关闭数据执行保护
    VC++开发垃圾文件清理软件(下)
    用完成端口开发大响应规模的Winsock应用程序
    去掉右键多余显卡菜单
    自动登陆系统
    查询数据库中所有表名和表中所有字段名
    单行编辑框文本垂直居中(包含计算字体高度)
  • 原文地址:https://www.cnblogs.com/kailing-con/p/4306350.html
Copyright © 2020-2023  润新知