永久链接: http://gaojingsong.iteye.com/blog/2285271
预览文章: 设计模式之代理模式
JDK内置的Proxy动态代理可以在运行时动态生成字节码,而没必要针对每个类编写代理类。中间主要使用到了一个接口InvocationHandler与Proxy.newProxyInstance静态方法,参数说明如下:
Proxy这个类的作用就是用来动态创建一个代理对象的类,它提供了许多的方法,但是我们用的最多的就是 newProxyInstance 这个方法:
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException
Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler.
这个方法的作用就是得到一个动态的代理对象,其接收三个参数,我们来看看这三个参数所代表的含义:
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) throws IllegalArgumentException
loader: 一个ClassLoader对象,定义了由哪个ClassLoader对象来对生成的代理对象进行加载
interfaces: 一个Interface对象的数组,表示的是我将要给我需要代理的对象提供一组什么接口,如果我提供了一组接口给它,那么这个代理对象就宣称实现了该接口(多态),这样我就能调用这组接口中的方法了
h: 一个InvocationHandler对象,表示的是当我这个动态代理对象在调用方法的时候,会关联到哪一个InvocationHandler对象上
Proxy已经设计得非常优美,但是还是有一点点小小的遗憾之处,那就是它始终无法摆脱仅支持interface代理的桎梏,因为它的设计注定了这个遗憾。
使用内置的Proxy实现动态代理有一个问题:被代理的类必须实现接口,未实现接口则没办法完成动态代理。
如果项目中有些类没有实现接口,则不应该为了实现动态代理而刻意去抽出一些没有实例意义的接口,通过cglib可以解决该问题。
CGLIB(Code Generation Library)是一个开源项目,是一个强大的,高性能,高质量的Code生成类库,它可以在运行期扩展Java类与实现Java接口,通俗说cglib可以在运行时动态生成字节码。cglib是一种流行的代理。
使用步骤:
1)引入maven坐标
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
2)编写业务类
3)开发代理类,实现MethodInterceptor接口
源码示例如下:
1)编写业务类(可以实现接口也可以不实现接口)
package demo;
//再定一个丑陋的王婆.
//王婆这个人老聪明了,她太老了.是个男人都看不上,但是她有智慧有经验呀,她做为一类女人的代理!
public class WangPo {
public void makeEyesWithMan() {
// 这么大年龄了谁看她抛媚眼?!
System.out.println("王婆抛媚眼..这么大年龄了谁看她抛媚眼?.");
}
}
2)开发代理类,实现MethodInterceptor接口
package demo;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
/*
* 动态代理类
* 实现了一个方法拦截器接口
*/
public class DynamicProxy implements MethodInterceptor {
// 被代理对象
Object targetObject;
//Generate a new class if necessary and uses the specified callbacks (if any) to create a new object instance.
//Uses the no-arg constructor of the superclass.
//动态生成一个新的类,使用父类的无参构造方法创建一个指定了特定回调的代理实例
public Object getProxyObject(Object object) {
this.targetObject = object;
//增强器,动态代码生成器
Enhancer enhancer=new Enhancer();
//回调方法
enhancer.setCallback(this);
//设置生成类的父类类型
enhancer.setSuperclass(targetObject.getClass());
//动态生成字节码并返回代理对象
return enhancer.create();
}
// 拦截方法
public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
// 被织入的横切内容,开始时间 before
long start = System.currentTimeMillis();
// 调用方法
Object result = methodProxy.invoke(targetObject, args);
// 被织入的横切内容,结束时间
Long span = System.currentTimeMillis() - start;
System.out.println("共用时:" + span);
return result;
} }
3)测试
package demo;
public class DemoMain {
/**
* @param args
*/
public static void main(String[] args) {
//ProxyWomen proxyWomen = new ProxyWomen();
//KindWomen kindWomen = (KindWomen) proxyWomen.bind(new PanJinLian());
//kindWomen.makeEyesWithMan();
WangPo wangpo = (WangPo) new DynamicProxy().getProxyObject(new WangPo());
wangpo.makeEyesWithMan();
}
}