spring AOP中的<aop:config>配置

aop:config配置

一个application可以包含多个“aop:config”,一个”aop:config”要按顺序配置pointcut, advisor 和aspect。


切面aspect, 用aop:aspect声明,比如:

<aop:config>

    <aop:aspect id = "aspect" ref = "myBean">

    ....

    </aop:aspect>

</aop:config>
<bean id = "myBean" class = "...">

    ....

</bean>

切入点可以在切面里面声明,也可以在aop:config里面声明,作用域不同。


切入点pointcut, 用aop:pointcut声明,例如:

<aop:config>

    <aop:pointcut id = "businessService" expression = "execution(* com.lzr.service.businessService.*.* (..))" />

</aop:config>

通知advice

before通知,在aop:aspect里用aop:before来声明,在匹配的方法执行之前进入,比如:

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

    <aop:before pointcut = "execution(* com.lzr.dao.*.*(..))" method = "doAcessCheck" />

    ....

</aop:aspect>

返回后通知after return advice,在匹配的方法完全执行之后才执行,例如:

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

    <aop:after-returning poingcut-ref = "dataAccessOperation"  method = "doAcessCheck" />

    ....
</aop:aspect>

如果方法dataAccessOperation里面有形参relVal,那么可以这样写:

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

    <aop:after-returning poingcut-ref = "dataAccessOperation"  returning = "reVal" method = "doAcessCheck" />

    ....
</aop:aspect>

抛出异常后通知after throwing advice,在匹配的方法抛出异常退出的时候执行。

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

    <aop:after-throwing poingcut-ref = "dataAccessOperation"  method = "doAcessCheck" />

    ....
</aop:aspect>

使用throwing属性来指定异常的名称

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

    <aop:after-throwing poingcut-ref = "dataAccessOperation" throwing = "dataAcessEx" method = "doAcessCheck" />

    ....
</aop:aspect>

后通知after advice

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

    <aop:after pointcut-ref = "dataAccessOperation" method="doReleaseLock"/>

    ....

</aop:aspect>

周围通知around advice

<aop:aspect id = "around advice" ref = "myBean">

    <aop:around pointcut-ref = "businessService" method = "doBasicProfiling">

    ....

    </aop:aspect>

    ....

</aop:aspect>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值