• 17. Spring AOP编程 切点表达式的抽取


    见文生义,其实就是偷懒。抽取你还不懂吗?

    正文:

    当多个增强的切点表达式相同时,可以将切点表达式进行抽取,在增强中使用 pointcut-ref 属性代替 pointcut 属性来引用抽
    取后的切点表达式。

    其实是在配置文件中:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    
    
    
                <bean id="target" class="com.bihu.aop.Target"></bean>
                <bean id="myAspect" class="com.bihu.aop.MyAspect"></bean>
    
    
                <aop:config>
                    <!--引用myAspect的Bean为切面对象-->
                    <aop:aspect ref="myAspect">
                      <aop:before method="before" pointcut="execution(* com.bihu.aop.*.*(..))"/>
                      <aop:after-returning method="afterReturning" pointcut="execution(* com.bihu.aop.*.*(..))"/>
                      <aop:around method="around" pointcut="execution(* com.bihu.aop.*.*(..))"/>
                      <aop:after method="after" pointcut="execution(* com.bihu.aop.*.*(..))"/>
                    </aop:aspect>
                </aop:config>
    
    </beans>

    我现在上面四个切点表达式都是一样的  ,我可以这样:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
        
                <bean id="target" class="com.bihu.aop.Target"></bean>
                <bean id="myAspect" class="com.bihu.aop.MyAspect"></bean>
        
                <aop:config>
                    
                    
                    <!--引用myAspect的Bean为切面对象-->
                         <aop:aspect ref="myAspect">
                        <!--对切点表达式的抽取-->
                        <aop:pointcut id="myPointcutExpression" expression="execution(* com.bihu.aop.*.*(..))"/>
                        <!--下面配置切点表达式 以及 切点的(通知)增强方法-->
                        <aop:before method="before" pointcut-ref="myPointcutExpression"/>
                      <aop:after-returning method="afterReturning"  pointcut-ref="myPointcutExpression"/>
                      <aop:around method="around" pointcut-ref="myPointcutExpression"/>
                      <aop:after method="after"  pointcut-ref="myPointcutExpression"/>
                    </aop:aspect>
                </aop:config>
    
    </beans>

    自己对比一下 会发现不一样的就是在 

    pointcut-ref  值是 你抽取 切点表达式的 id。

    本文来自博客园,作者:咸瑜,转载请注明原文链接:https://www.cnblogs.com/bi-hu/p/15043438.html

  • 相关阅读:
    移动语义
    unordered_map/unordered_set & unordered_multimap/unordered_multiset非关联容器
    C++构造函数和析构函数,以及构造函数特殊成员变量和函数的初始化
    Valgrind,内存调试工具
    堆排序,图解,C/C++实现
    哈希表概念和实现,C/C++实现
    AVL平衡二叉树实现,图解分析,C++描述,完整可执行代码
    二叉排序树,Binary_Sort_Tree,C++完整实现
    [Codeforces Round #656 (Div. 3)] (E. Directing Edges)拓扑排序
    最短路 2 [HDU
  • 原文地址:https://www.cnblogs.com/bi-hu/p/15043438.html
Copyright © 2020-2023  润新知