zoukankan      html  css  js  c++  java
  • Spring 学习——Spring AOP——AOP配置篇Advice(有参数传递)

    声明通知Advice

    • 配置方式(以前置通知为例子)
      • 方式一
        复制代码
        <aop:config>
        <aop:aspect id="ikAspectAop" ref="ikAspect">
        <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:pointcut>
        <aop:before method="aspectBefore" pointcut-ref="ikPoint"></aop:before>
        </aop:aspect>
        </aop:config>
        复制代码
        package com.jing.spring.aop;
        /** 
        * 业务代码
        */
        public class IKAspectBiz { public void add(String name){ System.out.println("IKAspectBiz.add"); } }
        package com.jing.spring.aop;
        import org.aspectj.lang.ProceedingJoinPoint;
        
        /**
         * 切面代码
         */
        public class IKAspect {
        
            public void aspectBefore(String name){
                System.out.println("IKAspect.aspectBefore,it~s 前置通知");
                System.out.println("IKAspect.aspectBefore,it~s 前置通知"+name);
            }
        }
        • 优点:前置通知、后置通知、环绕通知使用同一个切点时,配置一个<aop:poincut />即可,方便配置。
      • 方式二
        复制代码
        <aop:config>
                <aop:aspect id="ikAspectAop" ref="ikAspect">
                        <aop:before method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:before>
                </aop:aspect>
        </aop:config>
        复制代码
        package com.jing.spring.aop;
        /** 
        * 业务代码
        */
        public class IKAspectBiz { public void add(String name){ System.out.println("IKAspectBiz.add"); } }
        package com.jing.spring.aop;
        import org.aspectj.lang.ProceedingJoinPoint;
        
        /**
         * 切面代码
         */
        public class IKAspect {
        
            public void aspectBefore(String name){
                System.out.println("IKAspect.aspectBefore,it~s 前置通知");
                System.out.println("IKAspect.aspectBefore,it~s 前置通知"+name);
            }
        }
        • 优点:前置通知、后置通知、环绕通知使用不同切点时,不需要配置<aop:poincut />元素,可以直接在<aop:before />元素内配置切点。
      • Next
    • 前置通知(Before Advice)
      • 在切入点时机事务执行之前,执行通知
      • 方式一
        复制代码
         <aop:config>
             <aop:aspect id="ikAspectAop" ref="ikAspect">
                  <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:pointcut>
                  <aop:before method="aspectBefore" pointcut-ref="ikPoint"></aop:before>
             </aop:aspect>
         </aop:config>
        复制代码
      • 方式二
        复制代码
         <aop:config>
              <aop:aspect id="ikAspectAop" ref="ikAspect">
                   <aop:before method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:before>
              </aop:aspect>
         </aop:config>
        复制代码
      • Next
    • 后置通知(After Running Advice)
      • 在切入点时机事务执行之后,执行通知(前提:切入点执行时没有异常,否则不会执行)
      • 方式一
        复制代码
        <aop:config>
             <aop:aspect id="ikAspectAop" ref="ikAspect">
                  <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:pointcut>
                  <aop:after-returning method="aspectBefore" pointcut-ref="ikPoint"></aop:before>
             </aop:aspect>
         </aop:config>
        复制代码
      • 方式二
        复制代码
        <aop:config>
             <aop:aspect id="ikAspectAop" ref="ikAspect">
                  <aop:after-returning method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:before>
             </aop:aspect>
         </aop:config>
        复制代码
      • Next
    • 抛出异常通知(After Throwing Advice)
      • 若在切入点时机执行时抛出异常,执行通知。(执行异常通知,则不会执行后置通知)
      • 方式一
        复制代码
        <aop:config>
             <aop:aspect id="ikAspectAop" ref="ikAspect">
                  <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name) "></aop:pointcut>
                  <aop:after-throwing method="aspectBefore" pointcut-ref="ikPoint"></aop:before>
             </aop:aspect>
         </aop:config>
        复制代码
      • 方式二
        复制代码
        <aop:config>
             <aop:aspect id="ikAspectAop" ref="ikAspect">
                  <aop:after-throwing method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:before>
             </aop:aspect>
         </aop:config>
        复制代码
      • Next
    • 后通知(After Ending Advice)
      • 在切入点时机执行之后,执行通知。(切入点执行时,无论正常执行,还是抛出异常,都会执行通知,相当于try_catch_finally)。
      • 方式一
        复制代码
        <aop:config>
             <aop:aspect id="ikAspectAop" ref="ikAspect">
                  <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:pointcut>
                  <aop:after method="aspectBefore" pointcut-ref="ikPoint"></aop:before>
             </aop:aspect>
         </aop:config>
        复制代码
      • 方式二
        复制代码
        <aop:config>
             <aop:aspect id="ikAspectAop" ref="ikAspect">
                  <aop:after method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..)) and args(name)"></aop:before>
             </aop:aspect>
         </aop:config>
        复制代码
      • Next
      • 无论切面是否出现异常,后通知动作正常执行
    • 环绕通知(Around Advice)
      • 环绕通知在
      • 方式一
        复制代码
        <?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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd" >

        <bean id="ikAspect" class="com.jing.spring.aop.IKAspect"></bean>
        <bean id="ikAspectBiz" class="com.jing.spring.aop.IKAspectBiz"></bean>

        <aop:config>
        <aop:aspect id="ikAspectAop" ref="ikAspect">
        <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.aopParams(String,int)) and args(name,times)"></aop:pointcut>
           <aop:around method="aspectAroundParams" pointcut-ref="ikPoint"></aop:around>

        </aop:aspect>
        </aop:config>

        </beans>
        复制代码
        复制代码
        package com.jing.spring.aop;

        /**
        * 业务代码
        */
        public class IKAspectBiz {

        public void aopParams(String name,int times){
        System.out.println("IKAspectBiz.aopParams");
        }
        }
        复制代码
        package com.jing.spring.aop;
        import org.aspectj.lang.ProceedingJoinPoint;
        
        /**
         * 切面代码
         */
        public class IKAspect {
        public void aspectAroundParams(ProceedingJoinPoint pj,String name,int times){
        
                System.out.println("IKAspect.aspectAroud,its 环绕通知前:");
                System.out.println("IKAspect.aspectAroud,"+name+"-----"+times);
                try {
                    Object proceed = pj.proceed();
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
                System.out.println("IKAspect.aspectAroud,its 环绕通知后");
                System.out.println("IKAspect.aspectAroud,"+name+"-----"+times);
        
            }
        }
      • 方式二
        复制代码
        <?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.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd" >
        
                <bean id="ikAspect" class="com.jing.spring.aop.IKAspect"></bean>
                <bean id="ikAspectBiz" class="com.jing.spring.aop.IKAspectBiz"></bean>
        
                <aop:config>
                    <aop:aspect id="ikAspectAop" ref="ikAspect">
           <aop:around method="aspectAroundParams" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.aopParams(String,int)) and args(name,times)"></aop:around>
        </aop:aspect> </aop:config> </beans>
        复制代码
        复制代码
        复制代码
        package com.jing.spring.aop;

        /**
        * 业务代码
        */
        public class IKAspectBiz {

        public void aopParams(String name,int times){
        System.out.println("IKAspectBiz.aopParams");
        }
        }
        复制代码
        package com.jing.spring.aop;
        import org.aspectj.lang.ProceedingJoinPoint;
        
        /**
         * 切面代码
         */
        public class IKAspect {
        
            public void aspectAroundParams(ProceedingJoinPoint pj,String name,int times){
        
                System.out.println("IKAspect.aspectAroud,its 环绕通知前:");
                System.out.println("IKAspect.aspectAroud,"+name+"-----"+times);
                try {
                    Object proceed = pj.proceed();
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                }
                System.out.println("IKAspect.aspectAroud,its 环绕通知后");
                System.out.println("IKAspect.aspectAroud,"+name+"-----"+times);
        
            }
        }
      • Next

    谨记

    • 切面中的参数来源是从业务中取得的,也就是说,只有在切点中的参数,才会传送到相对应的切面中,所以,切面中方法参数和切点时机的参数要保持对应。
    <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.aopParams(String,int)) and args(name,times)"></aop:pointcut>
    <aop:around method="aspectAroundParams" pointcut-ref="ikPoint"></aop:around>
    • (以环绕通知举例)上述中的args(name,times)中的参数name对应切面IKAspect 类中的aspectAroundParams(ProceedingJoinPoint pj,String name,int times)方法中的参数,并且参数名称和顺序需要与切面中的通知方法参数名称和顺序一致。
    • (以环绕通知举例)上述中的aopParams(String,int)中的参数是对应业务IKAspectBiz 中的aopParams(String name,int times)方法中的参数类型。
  • 相关阅读:
    Type Safety and Type Inference
    LEARN SWIFT
    swift 类型备份
    Swift
    associatedtype关联类型
    深入理解 Swift 派发机制
    Swift中self和Self
    Postfix Self Expression
    CGContext与上下文
    eeee
  • 原文地址:https://www.cnblogs.com/zuiyue_jing/p/10445526.html
Copyright © 2011-2022 走看看