Aspectj提供的Advice类型
AspectJ提供不同的通知类型:
Before (前置通知),相当于BeforeAdvice
AfterReturning (后置通知),相当于AfterReturningAdvice
Around (环绕通知),相当于MethodInterceptor
AfterThrowing(抛出通知),相当于ThrowAdvice
After (最终final通知),不管是否异常,该通知都会执行
DeclareParents (引介通知),相当于IntroductionInterceptor
开发思路
第一步:确定目标对象,即确定bean对象
第二步:advice通知(编写)
第三步:配置切面(包括切入点),让切入点关联通知
XML中配置:
<!-- 配置aop -->
<aop:config>
<!-- 配置切点
expression:切点表达式
-->
<aop:pointcut expression="bean(*ServiceImpl)" id="pointCut"/>
<!--
配置通知
ref:通知类的bean id
-->
<aop:aspect ref="myAspect">
<!--
method:通知类中,前置通知对应的方法
pointcut-ref:指定切入点,使得通知可以作用到对应的方法上面去
-->
<aop:before method="before" pointcut-ref="pointCut"/>
<!--
returning:通知方法中用来表示目标方法执行完毕以后返回结果的形式参数的名称
-->
<aop:after-returning method="afterReturning" pointcut-ref="pointCut" returning="returnVal"/>
<aop:around method="around" pointcut-ref="pointCut"/>
<!--
throwing:异常通知方法中对应的异常对象名
-->
<aop:after-throwing method="afterThrowing" pointcut-ref="pointCut" throwing="ex"/>
<aop:after method="after" pointcut-ref="pointCut"/>
</aop:aspect>
</aop:config>