Spring AOP(2) --基于配置文件方式配置

1、spring-aop-xml.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

    <!--配置切面bean-->
    <bean id="loggingAspectXml" class="com.spring.aop.xml.LoggingAspect"/>

    <bean id="validationAspectXml" class="com.spring.aop.xml.ValidationAspect"/>

    <!--配置bean-->
    <bean id="mathCalculatorXml" class="com.spring.aop.xml.MathCalculatorImpl"/>


    <!--*************aop配置*****************-->
    <!--
        <aop:aspect/>和<aop:advisor/>的区别
        < aop:aspect>:定义切面(切面包括通知和切点)
        < aop:advisor>:定义通知器(通知器跟切面一样,也包括通知和切点)
        1、实现方式不同
          < aop:aspect>定义切面时,只需要定义一般的bean就行,
          而定义< aop:advisor>中引用的通知时,通知必须实现Advice接口。
        2、使用场景不同
          < aop:advisor>大多用于事务管理。

         总结:< aop:advisor>和< aop:aspect>其实都是将通知和切面进行了封装,原理基本上是一样的,只是使用的方式不同而已。
    -->

    <!--配置aop-->
    <aop:config>
        <!--配置切点表达式-->
        <aop:pointcut id="pointCut" expression="execution(* com.spring.aop.xml.MathCalculator.*(..))"/>

        <!--配置切面及通知-->
        <aop:aspect ref="loggingAspectXml" order="2">
            <!-- <aop:before method="beforeMethod" pointcut-ref="pointCut"/>
             <aop:after method="afterMethod" pointcut-ref="pointCut"/>
             <aop:after-returning method="afterReturning" pointcut-ref="pointCut" returning="result"/>
             <aop:after-throwing method="afterThrowing" pointcut-ref="pointCut" throwing="e"/>-->
            <aop:around method="aroundMethod" pointcut-ref="pointCut"/>
        </aop:aspect>

        <!--测试优先级,order-->
        <aop:aspect ref="validationAspectXml" order="1">
            <aop:before method="validateArgs" pointcut-ref="pointCut"/>
        </aop:aspect>

    </aop:config>

</beans>
作者:donleo123
本文如对您有帮助,还请多推荐下此文,如有错误欢迎指正,相互学习,共同进步。
原文地址:https://www.cnblogs.com/donleo123/p/14069617.html