JAVA的动态代理
代理模式
代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息、过滤消息、把消息转发给委托类,以及事后处理消息等。代理类与委托类之间通常会存在关联关系,一个代理类的对象与一个委托类的对象关联,代理类的对象本身并不真正实现服务,而是通过调用委托类的对象的相关方法,来提供特定的服务。
按照代理的创建时期,代理类可以分为两种。
静态代理:由程序员创建或特定工具自动生成源代码,再对其编译。在程序运行前,代理类的.class文件就已经存在了。
动态代理:在程序运行时,运用反射机制动态创建而成。
首先看一下静态代理:
package org.js.test;
/**
* Created with IntelliJ IDEA.
* User: dev02
* Date: 16-1-26
* Time: 下午5:55
* To change this template use File | Settings | File Templates.
*/
public class ProxyTest {
public static void main(String[] args){
CountImp countImp=new ProxyTest().new CountImp();
CountImp1 countImp1=new ProxyTest().new CountImp1();
CountProxy countProxy=new ProxyTest().new CountProxy(countImp);
countProxy.queryCount();
countProxy.updateCount();
CountProxy countProxy1=new ProxyTest().new CountProxy(countImp1);
countProxy1.queryCount();
countProxy1.updateCount();
}
public interface Count{
public void queryCount();
public void updateCount();
}
public class CountImp implements Count{
public void queryCount() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("queryCount:"+this.getClass().getName());
}
public void updateCount() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("updateCount:"+this.getClass().getName());
}
}
public class CountImp1 implements Count{
public void queryCount() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("queryCount:"+this.getClass().getName());
}
public void updateCount() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("updateCount:"+this.getClass().getName());
}
}
public class CountProxy implements Count{
private Count target;
public CountProxy(Count target) {
this.target = target;
}
public void queryCount() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("queryCount:"+this.getClass().getName());
this.target.queryCount();
}
public void updateCount() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("updateCount:"+this.getClass().getName());
this.target.updateCount();
}
}
}
观察代码可以发现每一个代理类只能为一个接口服务,这样一来程序开发中必然会产生过多的代理,而且,所有的代理操作除了调用的方法不一样之外,其他的操作都一样,则此时肯定是重复代码。解决这一问题最好的做法是可以通过一个代理类完成全部的代理功能,那么此时就必须使用动态代理完成。
再来看一下动态代理:
JDK动态代理中包含一个类和一个接口:
InvocationHandler接口:
public interface InvocationHandler {
public Object invoke(Object proxy,Method method,Object[] args) throws Throwable;
}
参数说明:
Object proxy:指被代理的对象。
Method method:要调用的方法
Object[] args:方法调用时所需要的参数
可以将InvocationHandler接口的子类想象成一个代理的最终操作类,替换掉ProxySubject。
Proxy类:
Proxy类是专门完成代理的操作类,可以通过此类为一个或多个接口动态地生成实现类,此类提供了如下的操作方法:
public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
参数说明:
ClassLoader loader:类加载器
Class<?>[] interfaces:得到全部的接口
InvocationHandler h:得到InvocationHandler接口的子类实例
动态代理
与静态代理类对照的是动态代理类,动态代理类的字节码在程序运行时由Java反射机制动态生成,无需程序员手工编写它的源代码。动态代理类不仅简化了编程工作,而且提高了软件系统的可扩展性,因为Java 反射机制可以生成任意类型的动态代理类。java.lang.reflect 包中的Proxy类和InvocationHandler 接口提供了生成动态代理类的能力。
动态代理示例:
package org.js.test;
import org.omg.CORBA.SystemException;
import org.omg.CORBA.portable.InputStream;
import org.omg.CORBA.portable.InvokeHandler;
import org.omg.CORBA.portable.OutputStream;
import org.omg.CORBA.portable.ResponseHandler;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* Created with IntelliJ IDEA.
* User: dev02
* Date: 16-1-26
* Time: 下午6:10
* To change this template use File | Settings | File Templates.
*/
public class DynProxyTest {
public static void main(String[] args){
CountImp countImp =new DynProxyTest().new CountImp();
BookImp bookImp =new DynProxyTest().new BookImp();
BookCountImp bookCountImp1 =new DynProxyTest().new BookCountImp();
DynProxy dynProxy =new DynProxyTest().new DynProxy();
Count DyncountImp = (Count)dynProxy.Binding(countImp);
DyncountImp.queryCount();
DyncountImp.updateCount();
Book DynbookImp = (Book)dynProxy.Binding(bookImp);
DynbookImp.addBook();
DynbookImp.deleteBook();
BookCountImp DynbookcountImp = (BookCountImp)dynProxy.Binding(bookCountImp1);//11
DynbookcountImp.addBook();
DynbookcountImp.deleteBook();
DynbookcountImp.queryCount();
DynbookcountImp.updateCount();
}
public interface Count{
public void queryCount();
public void updateCount();
}
public interface Book{
public void addBook();
public void deleteBook();
}
public interface BookCount extends Count,Book{
}
public class CountImp implements Count{
public void queryCount() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("queryCount:"+this.getClass().getName());
}
public void updateCount() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("updateCount:"+this.getClass().getName());
}
}
public class BookImp implements Book{
public void addBook() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("addBook:"+this.getClass().getName());
}
public void deleteBook() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("deleteBook:"+this.getClass().getName());
}
}
public class BookCountImp implements Count,Book{
public void addBook() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("addBook:"+this.getClass().getName());
}
public void deleteBook() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("deleteBook:"+this.getClass().getName());
}
public void queryCount() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("queryCount:"+this.getClass().getName());
}
public void updateCount() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("updateCount:"+this.getClass().getName());
}
}
public class DynProxy implements InvocationHandler {
private Object target;
public Object Binding(Object target){
this.target=target;
return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result=null;
System.out.println("begin.......");
result=method.invoke(target,args);
System.out.println("end.........");
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}
}
通过上面的例子可以看出,通过动态代理可以代理多个接口及实现类,解决了静态代理每次只能代理一个类的问题,但是需要注意的是,如果一个实现类实现了多个接口,则动态代理返回时需要强转到接口,而不是实现类,如上面例子中注释11处所示。
访问下一节Java动态代理2——阅读 http://my.oschina.net/guanhe/blog/610475