Spring AOP 02 基于注解和配置的AOP

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)注解和配置
注解:快速方便
配置:功能完善;重要的推荐使用用配置,不重要的用注解。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值