spring aop 2.0 编程

在spring2.0中,aop发生了很大的变化:
主要分为两大方面
1.支持简单的aop xml配置
2.支持@AspectJ的注释

先来看一看第一种情况:
申明一个aspect,在xml中的申明如下:


<aop:config>

  <aop:aspect id="myAspect" ref="aBean">

    ...

  </aop:aspect>

</aop:config>

 

<bean id="aBean" class="...">

  ...

</bean>

 

申明pointcut


<aop:config>

  <aop:pointcut id="businessService"

        expression="execution(* com.xyz.myapp.service.*.*(..))"/>

</aop:config>


申明advice


Before advice:

<aop:aspect id="beforeExample" ref="aBean">

    <aop:before

      pointcut-ref="dataAccessOperation"

      method="doAccessCheck"/>

</aop:aspect>

After returning advice:

<aop:aspect id="afterReturningExample" ref="aBean">

 

    <aop:after-returning

      pointcut-ref="dataAccessOperation"

      method="doAccessCheck"/>

         

    ...

   

</aop:aspect>

或者带有返回参数

<aop:aspect id="afterReturningExample" ref="aBean">

 

    <aop:after-returning

      pointcut-ref="dataAccessOperation"

      returning="retVal"

      method="doAccessCheck"/>

         

    ...

   

</aop:aspect>

 

After throwing advice:

<aop:aspect id="afterThrowingExample" ref="aBean">

 

    <aop:after-throwing

      pointcut-ref="dataAccessOperation"

      method="doRecoveryActions"/>

         

    ...

   

</aop:aspect>

或者带有throwing

<aop:aspect id="afterThrowingExample" ref="aBean">

 

    <aop:after-throwing

      pointcut-ref="dataAccessOperation"

      throwing="dataAccessEx"

      method="doRecoveryActions"/>

         

    ...

   

</aop:aspect>

After (finally) advice:

<aop:aspect id="afterFinallyExample" ref="aBean">

 

    <aop:after

      pointcut-ref="dataAccessOperation"

      method="doReleaseLock"/>

         

    ...

   

</aop:aspect>

Around advice:

<aop:aspect id="aroundExample" ref="aBean">

 

    <aop:around

      pointcut-ref="businessService"

      method="doBasicProfiling"/>

         

    ...

   

</aop:aspect>

Advice parameters:

<aop:before

  pointcut="Pointcuts.anyPublicMethod() and @annotation(auditable)"

  method="audit"

  arg-names="auditable"/>

 

对于引入接口(Introductions):


<aop:aspect id="usageTrackerAspect" ref="usageTracking">

  <aop:declare-parents

      types-matching="com.xzy.myapp.service.*+",

      implement-interface="UsageTracked"
      default-impl=" service.tracking.DefaultUsageTracked"/>

  <aop:before

    pointcut="com.xyz.myapp.SystemArchitecture.businessService()

              and this(usageTracked)"

    method="recordUsage"/>

</aop:aspect>


前面主要介绍了如何通过xml实现aop编程,下面主要介绍如何通过@AspectJ来实现。
为了使@AspectJ 支持生效,
需要做以下步骤:
在xml中设置

<aop:aspectj-autoproxy/>


或者
在xml中加入
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />

声明 aspect

<bean id="myAspect" class="org.xyz.NotVeryUsefulAspect">

   <!-- configure properties of aspect here as normal -->

</bean>

 

package org.xyz;

import org.aspectj.lang.annotation.Aspect;

 

@Aspect

public class NotVeryUsefulAspect {

 

}

声明 pointcut


@Pointcut("execution(* transfer(..))")

public void transfer() {}

 

声明 advice


Before advice:

@Before("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")

  public void doAccessCheck() {

    // ...

  }

After returning advice:

@AfterReturning("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")

  public void doAccessCheck() {

    // ...

  }

或者

@AfterReturning(
pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",
returning="retVal")

public void doAccessCheck(Object retVal) {

    // ...

  }

After throwing advice:

@AfterThrowing("SystemArchitecture.dataAccessOperation()")

  public void doRecoveryActions() {

    // ...

  }

或者

@AfterThrowing(

    pointcut=" SystemArchitecture.dataAccessOperation()",

    throwing="ex")

  public void doRecoveryActions(DataAccessException ex) {

    // ...

  }

After (finally) advice:

@After("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")

  public void doReleaseLock() {

    // ...

  }

Around advice:

@Around("com.xyz.myapp.SystemArchitecture.businessService()")

  public Object doBasicProfiling( ProceedingJoinPoint pjp) throws Throwable {

    // start stopwatch

    Object retVal = pjp.proceed();

    // stop stopwatch

    return retVal;

  }

Advice parameters:

@Before("com.xyz.myapp.SystemArchitecture.dataAccessOperation() &&" + "args(account,..)" )

public void validateAccount(Account account) {

  // ...

}

声明参数名称:

@Before(

   value="com.xyz.lib.Pointcuts.anyPublicMethod() && " +

         "@annotation(auditable)",

   argNames="auditable" )

public void audit(Auditable auditable) {

  AuditCode code = auditable.value();

  // ...

Advice 排序:

一般以声明的方法次序为先后

不同的 Advice ,通过实现 Ordered 接口,来排序

 

Introductions


用于引入新的接口

@Aspect

public class UsageTracking {

 

  @DeclareParents(value="com.xzy.myapp.service.*+",

                  defaultImpl=DefaultUsageTracked.class)

  public static UsageTracked mixin;

 

  @Before("com.xyz.myapp.SystemArchitecture.businessService() &&" +

          "this(usageTracked)")

  public void recordUsage(UsageTracked usageTracked) {

    usageTracked.incrementUseCount();

  }

 

}

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ourife/archive/2007/06/07/1641797.aspx

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值