1、基于注解的AOP步骤
(1)将切面类和目标类加入ioc容器中,@Component
(2)告诉Spring哪个是切面类,@Apsect
(3)在切面类中使用五个通知注解来配置切面中的这些通知方法该何时何地的运行
(4)开启基于注解的AOP功能。
2、基于配置的AOP
(1)配置bean
//目标类
<bean id="mathCalculator" class="SpringAop02.impl.MyMathCalculator">
</bean>
//两个切面类
<bean id="logUtils2" class="SpringAop02.utils.LogUtils">
</bean>
<bean id="validateApsect2" class="SpringAop02.utils.ValidateApsect">
</bean>
(2)AOP名称空间
<aop:config>
<aop:pointcut id="myPoint2" expression="execution(* SpringAop02.impl.*.*(..))"/>
<!-- 指定切面:@Apsect-->
<aop:aspect ref="logUtils2" order="1">
<aop:pointcut id="myPoint" expression="execution(* SpringAop02.impl.*.*(..))"/>
<!-- 配置哪个方法是前置通知:method指定方法名,logstart@Before(切入点表达式)-->
<aop:before method="LogStart" pointcut="execution(* SpringAop02.impl.*.*(..))"></aop:before>
<aop:after-returning method="LogReturn" pointcut-ref="myPoint" returning="result"></aop:after-returning>
<aop:after-throwing method="LogException" pointcut-ref="myPoint" throwing="exception"></aop:after-throwing>
<aop:after method="LogEnd" pointcut-ref="myPoint"></aop:after>
<aop:around method="myAround" pointcut-ref="myPoint2"></aop:around>
</aop:aspect>
<aop:aspect ref="validateApsect2" order="-1">
<aop:before method="LogStart" pointcut-ref="myPoint2"></aop:before>
<aop:after-returning method="LogReturn" pointcut-ref="myPoint2" returning="result"></aop:after-returning>
<aop:after-throwing method="LogException" pointcut-ref="myPoint2" throwing="exception"></aop:after-throwing>
<aop:after method="LogEnd" pointcut-ref="myPoint2"></aop:after>
</aop:aspect>
<!-- 在切面类中使用五个通知注解来配置切面中的这些通知方法该何时何地的运行-->
</aop:config>
(3)注解和配置
注解:快速方便
配置:功能完善;重要的推荐使用用配置,不重要的用注解。