开发者

How do you get an interface array (Dynamic Proxy)

开发者 https://www.devze.com 2023-03-04 17:52 出处:网络
I\'m pretty confused on dynamic proxies.I understand that I need a ProxyCreator class that will have a interfaceArray va开发者_C百科riable.I am just not sure how I would go about creating an interface

I'm pretty confused on dynamic proxies. I understand that I need a ProxyCreator class that will have a interfaceArray va开发者_C百科riable. I am just not sure how I would go about creating an interface 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).

It all described here.

0

精彩评论

暂无评论...
验证码 换一张
取 消