闲人野居
好好学习,天天向上
posts - 57,  comments - 137,  trackbacks - 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>

 
posted on 2006-09-15 09:41 布衣郎 阅读(1967) 评论(0)  编辑  收藏 所属分类: aopspring

只有注册用户登录后才能发表评论。


网站导航:
 

<2006年9月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(12)

随笔分类(59)

随笔档案(57)

blog

java

uml

搜索

  •  

积分与排名

  • 积分 - 355562
  • 排名 - 154

最新评论

阅读排行榜

评论排行榜