spring aop 2.0 编程

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

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

<aop:config><o:p></o:p>

  <aop:aspect id="myAspect" ref="aBean"><o:p></o:p>

    ...<o:p></o:p>

  </aop:aspect><o:p></o:p>

</aop:config>

<bean id="aBean" class="..."><o:p></o:p>

  ...<o:p></o:p>

</bean>
<o:p></o:p>

<o:p>
申明pointcut
</o:p>

<aop:config><o:p></o:p>

  <aop:pointcut id="businessService" <o:p></o:p>

        expression="execution(* com.xyz.myapp.service.*.*(..))"/><o:p></o:p>

</aop:config><o:p></o:p>


申明advice

Before advice<o:p></o:p>

<aop:aspect id="beforeExample" ref="aBean"><o:p></o:p>

    <aop:before <o:p></o:p>

      pointcut-ref="dataAccessOperation" <o:p></o:p>

      method="doAccessCheck"/><o:p></o:p>

</aop:aspect><o:p></o:p>

After returning advice:<o:p></o:p>

<aop:aspect id="afterReturningExample" ref="aBean"><o:p></o:p>

<o:p> </o:p>

    <aop:after-returning <o:p></o:p>

      pointcut-ref="dataAccessOperation" <o:p></o:p>

      method="doAccessCheck"/><o:p></o:p>

          <o:p></o:p>

    ...<o:p></o:p>

    <o:p></o:p>

</aop:aspect><o:p></o:p>

或者带有返回参数

<aop:aspect id="afterReturningExample" ref="aBean"><o:p></o:p>

<o:p> </o:p>

    <aop:after-returning <o:p></o:p>

      pointcut-ref="dataAccessOperation"<o:p></o:p>

      returning="retVal" <o:p></o:p>

      method="doAccessCheck"/><o:p></o:p>

          <o:p></o:p>

    ...<o:p></o:p>

    <o:p></o:p>

</aop:aspect><o:p></o:p>

<o:p> </o:p>

After throwing advice<o:p></o:p>

<aop:aspect id="afterThrowingExample" ref="aBean"><o:p></o:p>

<o:p> </o:p>

    <aop:after-throwing<o:p></o:p>

      pointcut-ref="dataAccessOperation" <o:p></o:p>

      method="doRecoveryActions"/><o:p></o:p>

          <o:p></o:p>

    ...<o:p></o:p>

    <o:p></o:p>

</aop:aspect><o:p></o:p>

或者带有throwing<o:p></o:p>

<aop:aspect id="afterThrowingExample" ref="aBean"><o:p></o:p>

<o:p> </o:p>

    <aop:after-throwing <o:p></o:p>

      pointcut-ref="dataAccessOperation"<o:p></o:p>

      throwing="dataAccessEx"<o:p></o:p>

      method="doRecoveryActions"/><o:p></o:p>

          <o:p></o:p>

    ...<o:p></o:p>

    <o:p></o:p>

</aop:aspect><o:p></o:p>

After (finally) advice<o:p></o:p>

<aop:aspect id="afterFinallyExample" ref="aBean"><o:p></o:p>

<o:p> </o:p>

    <aop:after<o:p></o:p>

      pointcut-ref="dataAccessOperation" <o:p></o:p>

      method="doReleaseLock"/><o:p></o:p>

          <o:p></o:p>

    ...<o:p></o:p>

    <o:p></o:p>

</aop:aspect><o:p></o:p>

Around advice<o:p></o:p>

<aop:aspect id="aroundExample" ref="aBean"><o:p></o:p>

<o:p> </o:p>

    <aop:around<o:p></o:p>

      pointcut-ref="businessService" <o:p></o:p>

      method="doBasicProfiling"/><o:p></o:p>

          <o:p></o:p>

    ...<o:p></o:p>

    <o:p></o:p>

</aop:aspect><o:p></o:p>

Advice parameters<o:p></o:p>

<aop:before<o:p></o:p>

  pointcut="Pointcuts.anyPublicMethod() and @annotation(auditable)"<o:p></o:p>

  method="audit"<o:p></o:p>

  arg-names="auditable"/>
<o:p></o:p>


对于引入接口(Introductions):

<aop:aspect id="usageTrackerAspect" ref="usageTracking"><o:p></o:p>

  <aop:declare-parents<o:p></o:p>

      types-matching="com.xzy.myapp.service.*+",<o:p></o:p>

      implement-interface="UsageTracked"
      default-impl=" service.tracking.DefaultUsageTracked"/><o:p></o:p>

  <aop:before<o:p></o:p>

    pointcut="com.xyz.myapp.SystemArchitecture.businessService()<o:p></o:p>

              and this(usageTracked)"<o:p></o:p>

    method="recordUsage"/><o:p></o:p>

</aop:aspect>

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

<aop:aspectj-autoproxy/>
<o:p></o:p>

或者
在xml中加入
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
<o:p>
声明 aspect<o:p></o:p></o:p>

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

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

</bean>

<o:p> </o:p>

package org.xyz;

import org.aspectj.lang.annotation.Aspect;

<o:p> </o:p>

@Aspect<o:p></o:p>

public class NotVeryUsefulAspect {

<o:p> </o:p>

}

声明 pointcut
<o:p></o:p>

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

public void transfer() {}


声明 advice

Before advice:<o:p></o:p>

@Before("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")<o:p></o:p>

  public void doAccessCheck() {<o:p></o:p>

    // ...<o:p></o:p>

  }<o:p></o:p>

After returning advice:<o:p></o:p>

@AfterReturning("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")<o:p></o:p>

  public void doAccessCheck() {<o:p></o:p>

    // ...<o:p></o:p>

  }<o:p></o:p>

或者

@AfterReturning(
pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()",
returning="retVal")<o:p></o:p>

public void doAccessCheck(Object retVal) {<o:p></o:p>

    // ...<o:p></o:p>

  }<o:p></o:p>

After throwing advice:<o:p></o:p>

@AfterThrowing("SystemArchitecture.dataAccessOperation()")<o:p></o:p>

  public void doRecoveryActions() {<o:p></o:p>

    // ...<o:p></o:p>

  }<o:p></o:p>

或者

@AfterThrowing(<o:p></o:p>

    pointcut=" SystemArchitecture.dataAccessOperation()",<o:p></o:p>

    throwing="ex")<o:p></o:p>

  public void doRecoveryActions(DataAccessException ex) {<o:p></o:p>

    // ...<o:p></o:p>

  }<o:p></o:p>

After (finally) advice:<o:p></o:p>

@After("com.xyz.myapp.SystemArchitecture.dataAccessOperation()")<o:p></o:p>

  public void doReleaseLock() {<o:p></o:p>

    // ...<o:p></o:p>

  }<o:p></o:p>

Around advice:<o:p></o:p>

@Around("com.xyz.myapp.SystemArchitecture.businessService()")<o:p></o:p>

  public Object doBasicProfiling( ProceedingJoinPoint pjp) throws Throwable {<o:p></o:p>

    // start stopwatch<o:p></o:p>

    Object retVal = pjp.proceed();<o:p></o:p>

    // stop stopwatch<o:p></o:p>

    return retVal;<o:p></o:p>

  }<o:p></o:p>

Advice parameters:<o:p></o:p>

@Before("com.xyz.myapp.SystemArchitecture.dataAccessOperation() &&" + "args(account,..)" )<o:p></o:p>

public void validateAccount(Account account) {<o:p></o:p>

  // ...<o:p></o:p>

}<o:p></o:p>

声明参数名称: <o:p></o:p>

@Before(<o:p></o:p>

   value="com.xyz.lib.Pointcuts.anyPublicMethod() && " + <o:p></o:p>

         "@annotation(auditable)",<o:p></o:p>

   argNames="auditable" )<o:p></o:p>

public void audit(Auditable auditable) {<o:p></o:p>

  AuditCode code = auditable.value();<o:p></o:p>

  // ...<o:p></o:p>

}  <o:p></o:p>

Advice 排序:<o:p></o:p>

一般以声明的方法次序为先后 <o:p></o:p>

不同的 Advice ,通过实现 Ordered 接口,来排序
<o:p></o:p>


Introductions

<o:p></o:p>

用于引入新的接口

@Aspect<o:p></o:p>

public class UsageTracking {<o:p></o:p>

<o:p> </o:p>

  @DeclareParents(value="com.xzy.myapp.service.*+",<o:p></o:p>

                  defaultImpl=DefaultUsageTracked.class)<o:p></o:p>

  public static UsageTracked mixin;<o:p></o:p>

  <o:p></o:p>

  @Before("com.xyz.myapp.SystemArchitecture.businessService() &&" +<o:p></o:p>

          "this(usageTracked)")<o:p></o:p>

  public void recordUsage(UsageTracked usageTracked) {<o:p></o:p>

    usageTracked.incrementUseCount();<o:p></o:p>

  }<o:p></o:p>

  <o:p></o:p>

}<o:p></o:p>

<o:p></o:p>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值