久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

java動(dòng)態(tài)代理怎么實(shí)現(xiàn)

共計(jì) 2820 個(gè)字符,預(yù)計(jì)需要花費(fèi) 8 分鐘才能閱讀完成。

Java 動(dòng)態(tài)代理可以通過以下幾種方式實(shí)現(xiàn):

  1. 使用 java.lang.reflect.Proxy 類:Proxy類提供了一個(gè)靜態(tài)方法 newProxyInstance(),可以通過傳入目標(biāo)對(duì)象的類加載器、目標(biāo)對(duì)象實(shí)現(xiàn)的接口和InvocationHandler 接口的實(shí)現(xiàn)類來創(chuàng)建代理對(duì)象。InvocationHandler接口的實(shí)現(xiàn)類負(fù)責(zé)處理代理對(duì)象的方法調(diào)用。代理對(duì)象在調(diào)用方法時(shí),會(huì)把方法調(diào)用轉(zhuǎn)發(fā)給 InvocationHandlerinvoke()方法,從而實(shí)現(xiàn)代理功能。
public interface Subject {void doSomething();
}

public class RealSubject implements Subject {public void doSomething() {System.out.println("RealSubject doSomething");
    }
}

public class MyInvocationHandler implements InvocationHandler {private Subject target;

    public MyInvocationHandler(Subject target) {this.target = target;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {System.out.println("Before method invoke");
        Object result = method.invoke(target, args);
        System.out.println("After method invoke");
        return result;
    }
}

public class Main {public static void main(String[] args) {Subject realSubject = new RealSubject();
        Subject proxySubject = (Subject) Proxy.newProxyInstance(realSubject.getClass().getClassLoader(),
                realSubject.getClass().getInterfaces(),
                new MyInvocationHandler(realSubject));
        proxySubject.doSomething();}
}
  1. 使用 CGLib 庫(kù):CGLib是一個(gè)基于 ASM 框架的字節(jié)碼生成庫(kù),它可以在運(yùn)行時(shí)動(dòng)態(tài)生成目標(biāo)對(duì)象的子類,并覆蓋其中的方法以實(shí)現(xiàn)代理功能。
public interface Subject {void doSomething();
}

public class RealSubject implements Subject {public void doSomething() {System.out.println("RealSubject doSomething");
    }
}

public class MyMethodInterceptor implements MethodInterceptor {public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {System.out.println("Before method invoke");
        Object result = proxy.invokeSuper(obj, args);
        System.out.println("After method invoke");
        return result;
    }
}

public class Main {public static void main(String[] args) {Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(RealSubject.class);
        enhancer.setCallback(new MyMethodInterceptor());
        Subject proxySubject = (Subject) enhancer.create();
        proxySubject.doSomething();}
}

需要注意的是,使用 CGLib 實(shí)現(xiàn)動(dòng)態(tài)代理時(shí),目標(biāo)對(duì)象不能是 final 類或者包含 final 方法,因?yàn)?CGLib 是通過生成目標(biāo)對(duì)象的子類來實(shí)現(xiàn)代理的。

  1. 使用 ByteBuddy 庫(kù):ByteBuddy是一個(gè)輕量級(jí)的字節(jié)碼操作庫(kù),它可以在運(yùn)行時(shí)動(dòng)態(tài)生成目標(biāo)對(duì)象的子類,并覆蓋其中的方法以實(shí)現(xiàn)代理功能。
public interface Subject {void doSomething();
}

public class RealSubject implements Subject {public void doSomething() {System.out.println("RealSubject doSomething");
    }
}

public class MyInterceptor implements MethodInterceptor {public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {System.out.println("Before method invoke");
        Object result = proxy.invokeSuper(obj, args);
        System.out.println("After method invoke");
        return result;
    }
}

public class Main {public static void main(String[] args) throws Exception {Class<? extends Subject> proxyClass = new ByteBuddy()
                .subclass(Subject.class)
                .method(any()).intercept(MethodDelegation.to(MyInterceptor.class))
                .make()
                .load(RealSubject.class.getClassLoader())
                .getLoaded();
        Subject proxySubject = proxyClass.getDeclaredConstructor().newInstance();
        proxySubject.doSomething();}
}

需要注意的是,使用 ByteBuddy 實(shí)現(xiàn)動(dòng)態(tài)代理時(shí),需要引入 net.bytebuddy:byte-buddynet.bytebuddy:byte-buddy-agent兩個(gè)依賴。

丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-12-13發(fā)表,共計(jì)2820字。
轉(zhuǎn)載說明:除特殊說明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請(qǐng)注明出處。
評(píng)論(沒有評(píng)論)
主站蜘蛛池模板: 建平县| 丰台区| 清原| 晴隆县| 马山县| 勐海县| 丰宁| 龙里县| 白水县| 和平县| 威海市| 浮梁县| 元氏县| 宜君县| 义马市| 喀喇沁旗| 合川市| 北票市| 华安县| 西乡县| 米脂县| 饶阳县| 苍山县| 潍坊市| 名山县| 轮台县| 隆尧县| 宁海县| 丹阳市| 安溪县| 东阿县| 洪泽县| 曲沃县| 扎兰屯市| 冀州市| 邛崃市| 安义县| 班玛县| 白水县| 牡丹江市| 容城县|