• 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
    本文如对您有帮助,还请多推荐下此文,如有错误欢迎指正,相互学习,共同进步。
  • 相关阅读:
    干货分享|安全测试起航之旅
    干货|宏巍软件之Java线程监控之旅
    大数据时代,业务运维驱动下的企业变革
    什么是业务运维,企业如何实现互联网+业务与IT的融合
    不谈业务运维的IT主管早晚被淘汰 这里是10条干货
    详解APM数据采样与端到端
    CTO对话:云端融合下的移动技术创新
    【1111元天天拿!充一送二玩真哒!】双十一任性活动
    引领手机流量营销 嘿嘿流量打造多场景专业服务
    “烧钱补贴”下的O2O该何去何从?
  • 原文地址:https://www.cnblogs.com/donleo123/p/14069617.html
Copyright © 2020-2023  润新知