璁捐妯″紡@绗?5绔狅細浠g悊妯″紡

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了璁捐妯″紡@绗?5绔狅細浠g悊妯″紡相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/jpg' title='jpg'>jpg   str   鍥剧墖   nts   tar   owa   瀛楄妭   pac   鍐呯綉   

涓€銆佷唬鐞嗘ā寮?Proxy)

浜屻€侀潤鎬佷唬鐞?/h2>

锛堜竴锛夐潤鎬佷唬鐮佹ā寮忕殑鍩烘湰浠嬬粛

闈欐€佷唬鐞嗗湪浣跨敤鏃?闇€瑕佸畾涔夋帴鍙f垨鑰呯埗绫?琚唬鐞嗗璞?鍗崇洰鏍囧璞?涓庝唬鐞嗗璞′竴璧峰疄鐜扮浉鍚岀殑鎺ュ彛鎴栬€呮槸缁ф壙鐩稿悓鐖剁被

  • 浠g爜瀹炵幇

鎺ュ彛锛欼TeacherDao

package com.gjxaiou.proxy.staticproxy;

//鎺ュ彛
public interface ITeacherDao {
    void teach(); // 鎺堣鐨勬柟娉?}

瀹炵幇鎺ュ彛锛?/p>

package com.gjxaiou.proxy.staticproxy;

public class TeacherDao implements ITeacherDao {

    @Override
    public void teach() {
        System.out.println(" 鑰佸笀鎺堣涓? 銆傘€傘€傘€傘€?quot;);
    }
}

浠g悊锛?/p>

package com.gjxaiou.proxy.staticproxy;

//浠g悊瀵硅薄,闈欐€佷唬鐞?public class TeacherDaoProxy implements ITeacherDao{
    
    private ITeacherDao target; // 鐩爣瀵硅薄锛岄€氳繃鎺ュ彛鏉ヨ仛鍚?    
    //鏋勯€犲櫒
    public TeacherDaoProxy(ITeacherDao target) {
        this.target = target;
    }
    
    @Override
    public void teach() {
        System.out.println("寮€濮嬩唬鐞? 瀹屾垚鏌愪簺鎿嶄綔銆傘€傘€傘€傘€?");//鏂规硶
        target.teach();
        System.out.println("鎻愪氦銆傘€傘€傘€傘€?quot;);//鏂规硶
    }
}

瀹㈡埛绔娇鐢細

package com.gjxaiou.proxy.staticproxy;

public class Client {

    public static void main(String[] args) {
        //鍒涘缓鐩爣瀵硅薄(琚唬鐞嗗璞?
        TeacherDao teacherDao = new TeacherDao();
        
        //鍒涘缓浠g悊瀵硅薄, 鍚屾椂灏嗚浠g悊瀵硅薄浼犻€掔粰浠g悊瀵硅薄
        TeacherDaoProxy teacherDaoProxy = new TeacherDaoProxy(teacherDao);
        
        //閫氳繃浠g悊瀵硅薄锛岃皟鐢ㄥ埌琚唬鐞嗗璞$殑鏂规硶
        //鍗筹細璋冪敤鐨勬槸浠g悊瀵硅薄鐨勬柟娉曪紝浠g悊瀵硅薄鍐嶅幓璋冪敤鐩爣瀵硅薄鐨勬柟娉?
        teacherDaoProxy.teach();
    }
}

锛堜笁锛夐潤鎬佷唬鐞嗕紭缂虹偣

  • 浼樼偣锛氬湪涓嶄慨鏀圭洰鏍囧璞$殑鍔熻兘鍓嶆彁涓? 鑳介€氳繃浠g悊瀵硅薄瀵圭洰鏍囧姛鑳芥墿灞?/p>

  • 缂虹偣锛氬洜涓轰唬鐞嗗璞¢渶瑕佷笌鐩爣瀵硅薄瀹炵幇涓€鏍风殑鎺ュ彛,鎵€浠ヤ細鏈夊緢澶氫唬鐞嗙被

  • 涓€鏃︽帴鍙e鍔犳柟娉?鐩爣瀵硅薄涓庝唬鐞嗗璞¢兘瑕佺淮鎶?/p>

锛堜竴锛夊姩鎬佷唬鐞嗘ā寮忕殑鍩烘湰浠嬬粛

  • 浠g悊瀵硅薄,涓嶉渶瑕佸疄鐜版帴鍙o紝浣嗘槸鐩爣瀵硅薄瑕佸疄鐜版帴鍙?/strong>锛屽惁鍒欎笉鑳界敤鍔ㄦ€佷唬鐞嗭紱

  • 浠g悊瀵硅薄鐨勭敓鎴愶紝鏄埄鐢?JDK 鐨?API锛屽姩鎬佺殑鍦ㄥ唴瀛樹腑鏋勫缓浠g悊瀵硅薄锛?/p>

  • 鍔ㄦ€佷唬鐞嗕篃鍙仛锛欽DK 浠g悊銆佹帴鍙d唬鐞嗭紱

锛堜簩锛塉DK 涓敓鎴愪唬鐞嗗璞$殑API

  • 浠g悊绫绘墍鍦ㄥ寘:java.lang.reflect.Proxy锛?/p>

  • JDK 瀹炵幇浠g悊鍙渶瑕佷娇鐢?newProxyInstance 鏂规硶,浣嗘槸璇ユ柟娉曢渶瑕佹帴鏀朵笁涓弬鏁?瀹屾暣鐨勫啓娉曟槸: static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces,InvocationHandler h )

  • 浠g爜瀹炵幇

鎺ュ彛鏂规硶锛?/p>

  package com.gjxaiou.proxy.dynamic;
  
  //鎺ュ彛
  public interface ITeacherDao {
  
    void teach(); // 鎺堣鏂规硶
    void sayHello(String name);
  }
  

鍏蜂綋鐨勫疄鐜扮被锛?/p>

  package com.gjxaiou.proxy.dynamic;
  
  public class TeacherDao implements ITeacherDao {
  
    @Override
    public void teach() {
        System.out.println(" 鑰佸笀鎺堣涓?... ");
    }
  
    @Override
    public void sayHello(String name) {
        System.out.println("hello " + name);
    }
  }
  

浠g悊绫伙細

  package com.gjxaiou.proxy.dynamic;
  
  import java.lang.reflect.InvocationHandler;
  import java.lang.reflect.Method;
  import java.lang.reflect.Proxy;
  
  public class ProxyFactory {
  
      //缁存姢涓€涓洰鏍囧璞?, Object
      private Object target;
  
      //鏋勯€犲櫒 锛?瀵箃arget(鐩爣瀵硅薄) 杩涜鍒濆鍖?      public ProxyFactory(Object target) {
          this.target = target;
      }
  
      //缁欑洰鏍囧璞$敓鎴愪竴涓唬鐞嗗璞?      public Object getProxyInstance() {
  
          //鏂规硶涓弬鏁拌鏄?        /*
         *  public static Object newProxyInstance(ClassLoader loader,
                                            Class<?>[] interfaces,
                                            InvocationHandler h)
                                            
              //1. ClassLoader loader 锛?鎸囧畾褰撳墠鐩爣瀵硅薄浣跨敤鐨勭被鍔犺浇鍣? 鑾峰彇鍔犺浇鍣ㄧ殑鏂规硶鍥哄畾
              //2. Class<?>[] interfaces: 鐩爣瀵硅薄瀹炵幇鐨勬帴鍙g被鍨嬶紝浣跨敤娉涘瀷鏂规硶纭绫诲瀷
              //3. InvocationHandler h : 浜嬫儏澶勭悊锛屾墽琛岀洰鏍囧璞$殑鏂规硶鏃讹紝浼氳Е鍙戜簨鎯呭鐞嗗櫒鏂规硶, 浼氭妸褰撳墠鎵ц鐨勭洰鏍囧璞℃柟娉曚綔涓哄弬鏁颁紶鍏?         */
          return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                  target.getClass().getInterfaces(),
                  new InvocationHandler() {
  
                      @Override
                      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                          System.out.println("JDK浠g悊寮€濮媬~");
                          //鍙嶅皠鏈哄埗璋冪敤鐩爣瀵硅薄鐨勬柟娉?                          Object returnVal = method.invoke(target, args);
                          System.out.println("JDK浠g悊鎻愪氦");
                          return returnVal;
                      }
                  });
      }
  }
  

鍏蜂綋鐨勪娇鐢?/p>

  package com.gjxaiou.proxy.dynamic;
  
  public class Client {
  
    public static void main(String[] args) {
        //鍒涘缓鐩爣瀵硅薄
        ITeacherDao target = new TeacherDao();
        
        //缁欑洰鏍囧璞★紝鍒涘缓浠g悊瀵硅薄, 鍙互杞垚 ITeacherDao
        ITeacherDao proxyInstance = (ITeacherDao)new ProxyFactory(target).getProxyInstance();
    
        // 涓嬮潰璇彞缁撴灉涓猴細proxyInstance=class com.sun.proxy.$Proxy0 琛ㄧず鍐呭瓨涓姩鎬佺敓鎴愪簡浠g悊瀵硅薄
        System.out.println("proxyInstance=" + proxyInstance.getClass());
        
        //閫氳繃浠g悊瀵硅薄锛岃皟鐢ㄧ洰鏍囧璞$殑鏂规硶
        proxyInstance.teach();
        
        proxyInstance.sayHello(" tom ");
    }
  }
  

鍥涖€丆glib 浠g悊

(浜? Cglib 浠g悊妯″紡瀹炵幇姝ラ

  • 闇€瑕佸紩鍏?cglib 鐨?jar 鏂囦欢
    • asm.jar
    • asm-commons.jar
    • asm-tree.jar
    • cglib.jar
  • 鍦ㄥ唴瀛樹腑鍔ㄦ€佹瀯寤哄瓙绫伙紝娉ㄦ剰浠g悊鐨勭被涓嶈兘涓?final锛屽惁鍒欐姤閿檍ava.lang.IllegalArgumentException:

  • 鐩爣瀵硅薄鐨勬柟娉曞鏋滀负 final/static锛岄偅涔堝氨涓嶄細琚嫤鎴?鍗充笉浼氭墽琛岀洰鏍囧璞¢澶栫殑涓氬姟鏂规硶锛?/p>

锛堜笁锛塁glib 浠g悊妯″紡搴旂敤瀹炰緥

  • 搴旂敤瀹炰緥瑕佹眰

    灏嗗墠闈㈢殑妗堜緥鐢?Cglib 浠g悊妯″紡瀹炵幇

  • 鎬濊矾鍥捐В(绫诲浘)

鎶€鏈浘鐗? src=

  • 浠g爜瀹炵幇+Debug 婧愮爜[寰?debug]

琚唬鐞嗙殑瀵硅薄锛?/p>

package com.gjxaiou.proxy.cglib;

public class TeacherDao {

    public String teach() {
        System.out.println(" 鑰佸笀鎺堣涓? 锛?鎴戞槸cglib浠g悊锛屼笉闇€瑕佸疄鐜版帴鍙?");
        return "hello";
    }
}

浠g悊宸ュ巶锛?/p>

package com.gjxaiou.proxy.cglib;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

// MethodIntercepter 鏄?cglib 涓殑
public class ProxyFactory implements MethodInterceptor {

    //缁存姢涓€涓洰鏍囧璞?    private Object target;
    
    //鏋勯€犲櫒锛屼綔鐢ㄦ槸锛氫紶鍏ヤ竴涓浠g悊鐨勫璞?    public ProxyFactory(Object target) {
        this.target = target;
    }

    //杩斿洖涓€涓唬鐞嗗璞?  鏄?target 瀵硅薄鐨勪唬鐞嗗璞?    public Object getProxyInstance() {
        //1. 鍒涘缓涓€涓伐鍏风被
        Enhancer enhancer = new Enhancer();
        //2. 璁剧疆鐖剁被
        enhancer.setSuperclass(target.getClass());
        //3. 璁剧疆鍥炶皟鍑芥暟
        enhancer.setCallback(this);
        //4. 鍒涘缓瀛愮被瀵硅薄锛屽嵆浠g悊瀵硅薄
        return enhancer.create();
    }
    
    //閲嶅啓  intercept 鏂规硶锛屼細璋冪敤鐩爣瀵硅薄鐨勬柟娉?    @Override
    public Object intercept(Object arg0, Method method, Object[] args, MethodProxy arg3) throws Throwable {
        System.out.println("Cglib浠g悊妯″紡 ~~ 寮€濮?quot;);
        Object returnVal = method.invoke(target, args);
        System.out.println("Cglib浠g悊妯″紡 ~~ 鎻愪氦");
        return returnVal;
    }
}

浣跨敤锛?/p>

package com.gjxaiou.proxy.cglib;

public class Client {

    public static void main(String[] args) {
        //鍒涘缓鐩爣瀵硅薄
        TeacherDao target = new TeacherDao();
        //鑾峰彇鍒颁唬鐞嗗璞★紝骞朵笖灏嗙洰鏍囧璞′紶閫掔粰浠g悊瀵硅薄
        TeacherDao proxyInstance = (TeacherDao)new ProxyFactory(target).getProxyInstance();

        //鎵ц浠g悊瀵硅薄鐨勬柟娉曪紝瑙﹀彂intecept 鏂规硶锛屼粠鑰屽疄鐜?瀵圭洰鏍囧璞$殑璋冪敤
        String res = proxyInstance.teach();
        System.out.println("res=" + res);
    }
}

浜斻€佸嚑绉嶅父瑙佺殑浠g悊妯″紡浠嬬粛鈥?鍑犵鍙樹綋

  • 闃茬伀澧欎唬鐞?/li>

鍐呯綉閫氳繃浠g悊绌块€忛槻鐏锛屽疄鐜板鍏綉鐨勮闂€?/p>

  • 缂撳瓨浠g悊

姣斿锛氬綋璇锋眰鍥剧墖鏂囦欢绛夎祫婧愭椂锛屽厛鍒扮紦瀛樹唬鐞嗗彇锛屽鏋滃彇鍒拌祫婧愬垯 ok,濡傛灉鍙栦笉鍒拌祫婧愶紝鍐嶅埌鍏綉鎴栬€呮暟鎹簱鍙栵紝鐒跺悗缂撳瓨銆?/p>

  • 杩滅▼浠g悊

杩滅▼瀵硅薄鐨勬湰鍦颁唬琛紝閫氳繃瀹冨彲浠ユ妸杩滅▼瀵硅薄褰撴湰鍦板璞℃潵璋冪敤銆傝繙绋嬩唬鐞嗛€氳繃缃戠粶鍜岀湡姝g殑杩滅▼瀵硅薄娌熼€氫俊鎭€?/p>

  • 鍚屾浠g悊锛氫富瑕佷娇鐢ㄥ湪澶氱嚎绋嬬紪绋嬩腑锛屽畬鎴愬绾跨▼闂村悓姝ュ伐浣滃悓姝ヤ唬鐞嗭細涓昏浣跨敤鍦ㄥ绾跨▼缂栫▼涓紝瀹屾垚澶氱嚎绋嬮棿鍚屾宸ヤ綔

以上是关于璁捐妯″紡@绗?5绔狅細浠g悊妯″紡的主要内容,如果未能解决你的问题,请参考以下文章

璁捐妯″紡鍩烘湰姒傚康杈ㄦ瀽

璁捐妯″紡璇剧▼ 璁捐妯″紡绮捐 10-2 澶栬妯″紡coding

璁捐妯″紡

璁捐妯″紡锛歴tate妯″紡

璁捐妯″紡涔嬬粨鏋勫瀷--瑁呴グ妯″紡

璁捐妯″紡绯诲垪| 澶栬锛堥棬闈級妯″紡