简体   繁体   English

如何获得接口阵列(动态代理)

[英]How do you get an interface array (Dynamic Proxy)

I'm pretty confused on dynamic proxies. 我对动态代理很困惑。 I understand that I need a ProxyCreator class that will have a interfaceArray variable. 我了解我需要一个具有interfaceArray变量的ProxyCreator类。 I am just not sure how I would go about creating an interface arrau. 我只是不确定如何创建接口arrau。 Also, can I get a simple explanation for how to do a dynamic proxy. 另外,我可以得到有关如何执行动态代理的简单说明。 Thanks again! 再次感谢!

This way: 这条路:

Closeable c = (Closeable) java.lang.reflect.Proxy.newProxyInstance(
    getClass().getClassLoader(),
    new Class[]{ Closeable.class },
    new MyHandler(obj));

// works! by MyHandler is called instead.
c.close();

So the required interfaces are passed as an array of classes, and MyHandler is InvocationHanlder, taking delegate object obj as a parameter (if needed). 因此,所需的接口作为类的数组传递,而MyHandler是InvocationHanlder,将委托对象obj作为参数(如果需要)。

It all described here . 这一切都在这里描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM