JDK的代理,实际上是对Java接口(Interface)的代理,而且只能对接口进行代理,达到的效果是,当调用指定接口的指定方法时,会执行特定的操作。例如,HelloService是一个接口,public String sayHello(String msg)是该接口的其中一个方法,当代码调用该接口的sayHello方法时,会执行规定好的动作。这里会有一个问题,HelloService接口的实现类是从哪里来的呢?这里HelloService接口的实现类是由代理动态生成的实现类。所以想为一个接口生成代理,需要先生成一个代理对象。
生成代理对象,需要 代理的接口 以及 java.lang.reflect.InvocationHandler的实现类。具体看下文。
本文的代码下载:JDK动态代理demo
定义接口
HelloService接口
package com.lan.service;
public interface HelloService {
String sayHello(String msg);
}
创建java.lang.reflect.InvocationHandler的实现类
代理对象实际上执行的是InvocationHandler实现类的invoke方法,所以创建代理对象前需要先创建InvocationHandler的实现类。
DefaultInvocationHandler类
package com.lan.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class DefaultInvocation implements InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("被代理的接口方法:" + method);
System.out.println("被代理的接口方法的入参:" + args);
Class<?> clazz = method.getDeclaringClass();
if(clazz.getName().equals("com.lan.service.HelloService")
&& method.getName().equals("sayHello")) { // 还有method的入参个数、入参类型等等是否一直,此处忽略
return "hi, receive msg is " + args[0]; // 调用sayHello接口的返回值
}
return null;
}
}
生成接口的代理对象
生成代理对象,通过调用Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) 方法即可,入参说明如下:
ClassLoader loader:要加载代理对象类的类加载器
Class<?>[] interfaces:要被代理的接口集
InvocationHandler h:InvocationHandler的实现类,调用代理对象的方式时,实际上都是执行了该实现类的invoke方法
生成HelloService接口的代理对象
public static HelloService getHelloService() {
// InvocationHandler的实现类
InvocationHandler invocationHandler = new DefaultInvocation();
// 要加载代理对象类的类加载器
ClassLoader classLoader = HelloService.class.getClassLoader();
// 要被代理的接口
Class<?>[] interfaces = new Class<?>[] { HelloService.class };
// 生成HelloService接口的代理对象
HelloService helloService = (HelloService) Proxy.newProxyInstance(
classLoader, interfaces, invocationHandler);
return helloService;
}
通过代理对象调用接口
package com.lan;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import com.lan.proxy.DefaultInvocation;
import com.lan.service.HelloService;
public class App {
public static void main(String[] args) {
HelloService helloService = getHelloService();
String result = helloService.sayHello("hello, test");
System.out.println("result: " + result);
}
public static HelloService getHelloService() {
// InvocationHandler的实现类
InvocationHandler invocationHandler = new DefaultInvocation();
// 要加载代理对象类的类加载器
ClassLoader classLoader = HelloService.class.getClassLoader();
// 要被代理的接口
Class<?>[] interfaces = new Class<?>[] { HelloService.class };
// 生成HelloService接口的代理对象
HelloService helloService = (HelloService) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
return helloService;
}
}
运行结果
被代理的接口方法:public abstract java.lang.String com.lan.service.HelloService.sayHello(java.lang.String)
被代理的接口方法的入参:[Ljava.lang.Object;@6bc7c054
result: hi, receive msg is hello, test
扩展
JDK的代理,本质上是对Java接口(interface)的代理。
但是,若想对实现类的代理,可以在InvocationHandler实现类的构造方法传入对应的实现类对象,这样就可以在invoke方法的任意位置操作实现类,达到增强效果。
动态代理还常用于RPC的实现。客户端调用接口的方法,接口的实现类在服务端,看似是客户端能直接调用服务端的实现类方法,实际上是客户端生成了接口的代理对象,在执行InvocationHandler的invoke方法时,将客户端要执行的接口(interface)、方法(method)、入参(args)通过网络通讯发送给服务端,服务端找到接口的实现类对象,然后调用客户端指定的方法,并将方法的返回结果响应给客户端。其中的网络通讯还涉及到对象的序列化和反序列化。所以通常RPC的实现,由这三个关键的技术构成:动态代理、序列化与反序列化、网络通讯