基于XML的Spring AOP实现demo

作者: MacSam | 来源:发表于2016-06-02 11:19 被阅读93次

    接上篇基于注解的demo, 相关AOP的介绍就不复述了,详情可以点击下方链接,http://www.jianshu.com/p/66d21dae6a68

    • 运行环境 : 在之前写的环境中http://www.jianshu.com/p/dc9ac263e60d

    • 将上篇中的AspectjLog的注解都去掉,其他不变

    • 在my-spring-servlet.xml中,加入aop相关的命名空间.

    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"
    
    • 在my-spring-servlet.xml中加入切点相关的定义
        <!--启用AspectJ自动代理-->
        <aop:aspectj-autoproxy/>
    
        <bean id="AspectjLog" class="com.sam.aop.aspectj.AspectjLog"/>
        <aop:config proxy-target-class="true">
            <aop:aspect id="log" ref="AspectjLog">
                <aop:pointcut id="logAop"
                              expression="execution(* com.sam.aop.service.AspectService.sayHello(..)) and args(email)"/>
                <aop:before method="logBefore" pointcut-ref="logAop"/>
                <aop:after method="logAfter" pointcut-ref="logAop"/>
                <aop:after-returning method="logAfterReturning" pointcut-ref="logAop"/>
                <aop:after-throwing method="logAfterThrow" pointcut-ref="logAop"/>
                <aop:around method="logAfterThrow"  pointcut-ref="logAop"/>
            </aop:aspect>
        </aop:config>
    

    相关文章

      网友评论

        本文标题:基于XML的Spring AOP实现demo

        本文链接:https://www.haomeiwen.com/subject/tfbhdttx.html