一个application可以包含多个“aop:config”,一个”aop:config”要按顺序配置pointcut, advisor 和aspect。
切面aspect, 用aop:aspect声明,比如:
<aop:config>
<aop:aspect id = "aspect" ref = "myBean">
....
aop:aspect>
aop:config>
id = "myBean" class = "...">
....
切入点可以在切面里面声明,也可以在aop:config里面声明,作用域不同。
切入点pointcut, 用aop:pointcut声明,例如:
<aop:config>
<aop:pointcut id = "businessService" expression = "execution(* com.lzr.service.businessService.*.* (..))" />
aop:config>
before通知,在aop:aspect里用aop:before来声明,在匹配的方法执行之前进入,比如:
id = "beforeExample" ref = "myBean">
before pointcut = "execution(* com.lzr.dao.*.*(..))" method = "doAcessCheck" />
....
返回后通知after return advice,在匹配的方法完全执行之后才执行,例如:
id = "afterReturningExample" ref = "myBean">
after-returning poingcut-ref = "dataAccessOperation" method = "doAcessCheck" />
....
如果方法dataAccessOperation里面有形参relVal,那么可以这样写:
id = "afterReturningExample" ref = "myBean">
after-returning poingcut-ref = "dataAccessOperation" returning = "reVal" method = "doAcessCheck" />
....
抛出异常后通知after throwing advice,在匹配的方法抛出异常退出的时候执行。
id = "afterThrowingExample" ref = "myBean">
after-throwing poingcut-ref = "dataAccessOperation" method = "doAcessCheck" />
....
使用throwing属性来指定异常的名称
id = "afterThrowingExample" ref = "myBean">
after-throwing poingcut-ref = "dataAccessOperation" throwing = "dataAcessEx" method = "doAcessCheck" />
....
后通知after advice
id = "afterFinallyExample" ref = "myBean">
after pointcut-ref = "dataAccessOperation" method="doReleaseLock"/>
....
周围通知around advice
id = "around advice" ref = "myBean">
around pointcut-ref = "businessService" method = "doBasicProfiling">
....
....