Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入,这里跳过,而对于AOP来说,就是java的动态代理机制。
动态代理分为两大类:
- 基于接口 – JDK动态代理(主要讲解)
- 基于类 – cglib
- java 字节码实现 – javasist
在Jdk 的动态代理机制中,有两个重要的类或接口,一个是 InvocationHandler(Interface)、另一个则是 Proxy(Class)。
首先先看看官方描述:
InvocationHandler
InvocationHandler is the interface implemented by the invocation handler of a proxy instance.
Each proxy instance has an associated invocation handler. When a method is invoked on a proxy instance, the method invocation is encoded and dispatched to the invoke method of its invocation handler.
翻译下:
InvocationHandler是由代理实例的调用处理程序实现的接口。
每个代理实例都有一个关联的调用处理程序。 在代理实例上调用方法时,该方法调用将被编码并分派到其调用处理程序的 invoke 方法。
既然如此我们看下 invoke 方法
Object invoke(Object proxy, Method method, Object[] args) throws Throwable
proxy: 指代我们所代理的那个真实对象
method: 指代的是我们所要调用真实对象的某个方法的Method对象
args: 指代的是调用真实对象某个方法时接受的参数
Proxy
Proxy provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods.
翻译下:
代理提供了用于创建动态代理类和实例的静态方法,它还是由这些方法创建的所有动态代理类的超类。
Proxy 中有很多方法,看下最常用的 newProxyInstance 方法:
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException
loader: 一个ClassLoader对象,定义了由哪个ClassLoader对象来对生成的代理对象进行加载
interfaces: 一个Interface对象的数组,表示的是我将要给我需要代理的对象提供一组什么接口,如果我提供了一组接口给它,那么这个代理对象就宣称实现了该接口(多态),这样我就能调用这组接口中的方法了
h: 一个InvocationHandler对象,表示的是当我这个动态代理对象在调用方法的时候,会关联到哪一个InvocationHandler对象上
下面通过一个实例来看看动态代理:
// 定义一个接口:Rent
public interface Rent
{
public void rent();
}
// 定义一个真实对象,既需要代理的对象:Host
public class Host implements Rent
{
@Override
public void rent(){
System.out.println("I want to rent my house");
}
}
// 定义一个代理对象
public class DynamicProxy implements InvocationHandler
{
// 这个就是我们要代理的真实对象
private Object host;
// 构造方法,给我们要代理的真实对象赋初值
public DynamicProxy(Object host)
{
this.host= host;
}
@Override
public Object invoke(Object object, Method method, Object[] args) throws Throwable
{
// 在代理真实对象前我们可以添加一些自己的操作
System.out.println("before rent house ...");
System.out.println("Method name: " + method.getName());
// 当代理对象调用真实对象的方法时,其会自动的跳转到代理对象关联的handler对象的invoke方法来进行调用
method.invoke(host, args);
// 在代理真实对象后我们也可以添加一些自己的操作
System.out.println("after rent house ...");
return null;
}
}
public class ProxyClient {
public static void main(String[] args) {
// 需要代理的真实对象
Host host = new Host();
// 要代理哪个真实对象,就将该对象传进去,最后是通过该真实对象来调用其方法的
InvocationHandler handler = new DynamicProxy(host);
/*
* 通过Proxy的newProxyInstance方法来创建我们的代理对象
* 第一个参数 handler.getClass().getClassLoader() ,使用handler这个类的ClassLoader对象来加载代理对象
* host.getClass().getInterfaces(),为代理对象提供的接口是真实对象所实行的接口,表示要代理的是该真实对象,这样就能调用这组接口中的方法了
* 第三个参数handler, 将这个代理对象关联到了上方的 InvocationHandler 这个对象上
*/
Rent rent = (Rent) Proxy.newProxyInstance(handler.getClass().getClassLoader(), host.getClass().getInterfaces(), handler);
System.out.println(host.getClass().getName());
rent.rent();
}
}
控制台的输出:
com.sun.proxy.$Proxy0
出租房子前 ...
Method name: rent
出租房子
出租房子后 ...
解释下 com.sun.proxy.$Proxy0:
通过 Proxy.newProxyInstance 创建的代理对象是在jvm运行时动态生成的一个对象,它并不是我们的InvocationHandler类型,也不是我们定义的那组接口的类型,而是在运行是动态生成的一个对象,并且命名方式都是这样的形式,以$开头,proxy为中,最后一个数字表示对象的标号。