Spring 3 AOP实现方式

我们可以通过三种方式来使用Spring AOP,它们分别是:@Aspect-based(Annotation),Schema-based(XML),以及底层的Spring AOP API

底层的Spring AOP API比较复杂,下面只介绍@Aspect-based(注解方式)和Schema-based(声明方式)

两种方式比较:

Spring AOP和AspectJ的比较
   
    Spring AOP比完全使用AspectJ更加简单, 因为它不需要引入AspectJ的编译器/织
入器到你开发和构建过程中。 如果你仅仅需要在Spring bean上通知执行操作,那么Spring
AOP是合适的选择。 如果你需要通知domain对象或其它没有在Spring容器中管理的任意对
象,那么你需要使用AspectJ。 如果你想通知除了简单的方法执行之外的连接点(如:调用
连接点、字段get或set的连接点等等), 也需要使用AspectJ。

     XML风格 = 采用声明形式实现Spring AOP
    AspectJ风格 = 采用注解形式实现Spring AOP


    先前写的了两篇博文,描述了 XML风格 和 AspectJ风格的使用示例,有兴趣可看看:

    XML风格:http://xtu-xiaoxin.iteye.com/admin/blogs/630787
   AspectJ风格:http://xtu-xiaoxin.iteye.com/blog/630206

    1. 首先,对注解的支持是在Java5版本以后,所以,如果你使用的是java5版本以下的JVM,
不用考虑,必须选择XML风格 (XML配置形式的),而非注解形式(AspectJ风格)的。

    2. 使用XML风格,则所有的切面、切点、通知等配置都写在一个或几个Spring配置文件里。
这样的好处是,从配置文件中,就可以很清晰的看出系统中的有哪些切面,某个切面里使用那个的
通知(advice)以及通知(advice)作用的切点。而在AspectJ风格中,在java程序中标识切面
则显得凌乱、模糊。

    在什么情况下使用注解形式的AOP?或者说使用注解来实现AOP有哪些优点呢?
   
    1. XML风格的AOP仅仅支持"singleton"切面实例模型,而采用AspectJ风格的AOP则
没有这个限制。

    2.XML风格的AOP中是不支持命名连接点的声明,而采用AspectJ风格的AOP则没有这个限制。不太理解的看下面实例代码:
  在@AspectJ风格中我们可以编写如下的内容:
  

Java代码   收藏代码
  1. @Pointcut(execution(* get*()))  
  2.                 public void propertyAccess() {}  
  3.                   
  4.                 @Pointcut(execution(org.xyz.Account+ *(..))  
  5.                 public void operationReturningAnAccount() {}  
  6.                   
  7.                 @Pointcut(propertyAccess() && operationReturningAnAccount())  
  8.             public void accountPropertyAccess() {}  
@Pointcut(execution(* get*()))
                public void propertyAccess() {}
                
                @Pointcut(execution(org.xyz.Account+ *(..))
                public void operationReturningAnAccount() {}
                
                @Pointcut(propertyAccess() && operationReturningAnAccount())
            public void accountPropertyAccess() {}


在XML风格中,我们不能使用'&&'字符来连接命名连接点,如下:
Java代码   收藏代码
  1. <aop:pointcut id="propertyAccess"  
  2.                 expression="execution(* get*())"/>  
  3.                   
  4.                 <aop:pointcut id="operationReturningAnAccount"  
  5.             expression="execution(org.xyz.Account+ *(..))"/>  
  6.             <!-- 错误的配置  -->  
  7.           <aop:pointcut id="accountPropertyAccess"  
  8.             expression="propertyAccess && operationReturningAnAccount"/>  
<aop:pointcut id="propertyAccess"
                expression="execution(* get*())"/>
                
                <aop:pointcut id="operationReturningAnAccount"
            expression="execution(org.xyz.Account+ *(..))"/>
            <!-- 错误的配置  -->
          <aop:pointcut id="accountPropertyAccess"
            expression="propertyAccess && operationReturningAnAccount"/>



注意:
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值