成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

責(zé)任鏈模式在SpringAOP中怎么用-創(chuàng)新互聯(lián)

小編給大家分享一下責(zé)任鏈模式在SpringAOP中怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

我們提供的服務(wù)有:網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、科爾沁ssl等。為1000多家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的科爾沁網(wǎng)站制作公司

  當(dāng)一個對象在一條鏈上被多個攔截器攔截處理時,我們這樣的設(shè)計(jì)模式稱為責(zé)任鏈模式,它用于一個對象在多個角色中傳遞的場景。

  SpringAOP就是利用動態(tài)代理和責(zé)任鏈模式實(shí)現(xiàn)的,當(dāng)一個切面有多個織入時,這些需要織入的方法就形成了一個責(zé)任鏈,就像Filter鏈一樣。

  下面就模擬一下springaop中的責(zé)任鏈:

  接口:

  public interface Joinpoint {

  Object proceed() throws Throwable;

  }

  public interface MethodInvocation extends Joinpoint {

  }

  定義攔截器接口

  public interface MethodInterceptor {

  Object invoke(MethodInvocation mi) throws Throwable;

  }

  定義前置通知,在目標(biāo)方便調(diào)用前執(zhí)行通知:

  public class MethodBeforeAdviceInterceptor implements MethodInterceptor{

  @Override

  public Object invoke(MethodInvocation mi) throws Throwable {

  System.out.println("I am BeforeAdvice");

  return mi.proceed();

  }

  }

  定義后置通知,在目標(biāo)方法完成后執(zhí)行通知:

  public class AspectJAfterAdvice implements MethodInterceptor {

  @Override

  public Object invoke(MethodInvocation mi) throws Throwable {

  Object var;

  try {

  var = mi.proceed();

  }finally {

  System.out.println("I am AfterAdvice");

  }

  return var;

  }

  }

  中間類,攔截器鏈調(diào)用邏輯:

  public class ReflectiveMethodInvocation implements MethodInvocation{

  List methodInterceptors;

  public ReflectiveMethodInvocation(List methodInterceptors) {

  this.methodInterceptors = methodInterceptors;

  }

  private int index = -1;

  @Override

  public Object proceed() throws Throwable {

  Object var = null;

  if (index == this.methodInterceptors.size()-1) {

  System.out.println("真正的目標(biāo)方法");

  return new String("ha");

  }else{

  var = methodInterceptors.get(++index).invoke(this);

  }

  return var;

  }

  }

  測試類:

  public class Test {

  public static void main(String[] args) throws Throwable {

  AspectJAfterAdvice aspectJAfterAdvice = new AspectJAfterAdvice();

  MethodBeforeAdviceInterceptor methodBeforeAdviceInterceptor = new MethodBeforeAdviceInterceptor();

  List methodInterceptors = new ArrayList<>();

  methodInterceptors.add(methodBeforeAdviceInterceptor);

  methodInterceptors.add(aspectJAfterAdvice);

  ReflectiveMethodInvocation reflectiveMethodInvocation = new ReflectiveMethodInvocation(methodInterceptors);

  reflectiveMethodInvocation.proceed();

  }鄭州好的婦科醫(yī)院 http://www.zzkedayy.com/

  }

  執(zhí)行結(jié)果:

  I am BeforeAdvice

  真正的目標(biāo)方法

  I am AfterAdvice

  下面是springAOP中的源碼:

  首先看JdkDynamicAopProxy類中的invoke方法:

  final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable

  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

  Object oldProxy = null;

  boolean setProxyContext = false;

  TargetSource targetSource = this.advised.targetSource;

  Class targetClass = null;

  Object target = null;

  Integer var10;

  try {

  if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {

  Boolean var20 = this.equals(args[0]);

  return var20;

  }

  if (this.hashCodeDefined || !AopUtils.isHashCodeMethod(method)) {

  if (method.getDeclaringClass() == DecoratingProxy.class) {

  Class var18 = AopProxyUtils.ultimateTargetClass(this.advised);

  return var18;

  }

  Object retVal;

  if (!this.advised.opaque && method.getDeclaringClass().isInterface() && method.getDeclaringClass().isAssignableFrom(Advised.class)) {

  retVal = AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);

  return retVal;

  }

  if (this.advised.exposeProxy) {

  oldProxy = AopContext.setCurrentProxy(proxy);

  setProxyContext = true;

  }

  target = targetSource.getTarget();

  if (target != null) {

  targetClass = target.getClass();

  }

以上是“責(zé)任鏈模式在SpringAOP中怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道!

當(dāng)前題目:責(zé)任鏈模式在SpringAOP中怎么用-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://jinyejixie.com/article48/disihp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、標(biāo)簽優(yōu)化ChatGPT、Google、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站排名

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司
南丹县| 兴和县| 温宿县| 台安县| 贵南县| 蒲城县| 清河县| 姚安县| 江北区| 盱眙县| 称多县| 广昌县| 富源县| 泰州市| 遵义县| 海伦市| 永善县| 五峰| 永城市| 唐山市| 乐业县| 苗栗县| 威海市| 兴宁市| 丘北县| 东兰县| 绥江县| 尼木县| 贵定县| 广安市| 察哈| 杭锦后旗| 洛阳市| 沅陵县| 安陆市| 广饶县| 泗水县| 怀柔区| 伊宁县| 商洛市| 东至县|