日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring 学习——Spring AOP——AOP配置篇Advice(有参数传递)

發布時間:2023/12/20 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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/beanshttp://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)方法中的參數類型。

轉載于:https://www.cnblogs.com/zuiyue_jing/p/10445526.html

總結

以上是生活随笔為你收集整理的Spring 学习——Spring AOP——AOP配置篇Advice(有参数传递)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。