首先您需要以某种方式检索界面。然后像newProxyInstance中提到的那样创建代理。最后,您可以调用接口上的方法或将代理发布到某个服务定位器或类似服务器。
Class<?> unknownInterface = ClassLoader.getSystemClassLoader().loadClass("bar.UnknownInterface");
Object proxy = Proxy.newProxyInstance(unknownInterface.getClassLoader(),
new Class[] { unknownInterface },
new Handler());
unknownInterface.getMethod("someMethod", String.class).invoke(proxy, "hello");
// other way to call it:
// ((UnknownInterface) proxy).someMethod("hello");
处理程序类代表要提供实现:
public class Handler implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("someMethod")) {
System.out.println("this is the business logic of `someMethod`");
System.out.println("argument: " + args[0]);
return null;
}
return null;
}
}
什么是这里的缺点:您需要检索您的接口的Class对象
- 。可能你需要它的名字。
- a)您需要知道方法的名称和参数
- b)或者如果您知道方法的参数类型,您可以按类型匹配它们并忽略名称,例如基于this tutorial about proxies
args.length == 1 && args[0].getClass == String.class