静态代理:

例子:

接口:

public interface InterfaceBase {
void proxy();
}

接口实现类:

public class InterfaceBaseReal implements InterfaceBase{
public void proxy() {
System.out.println("InterfaceBase......");
}
}

代理类:

public class InterfaceBaseRealProxy  implements InterfaceBase{
//调用之前的功能
private InterfaceBaseReal interfaceBaseReal;
//通过构造函数进行被代理类的初始化
public InterfaceBaseRealProxy(InterfaceBaseReal interfaceBaseReal) {
this.interfaceBaseReal = interfaceBaseReal;
} public void proxy() {
System.out.println("InterfaceBaseRealProxy....before");
//调用被代理类的功能,保留原来的不变
interfaceBaseReal.proxy();
System.out.println("InterfaceBaseRealProxy....after");
}
}

动态代理:

动态代理的实现步骤:
必须有的类:被代理的类
被代理的类所实现的接口
实现InvocationHandler接口的类
流程:实现IncationHandler接口,
接口要求:通过构造函数传入被代理的类
在重写invoke函数里面进行被代理类方法的调用
编写增强逻辑
通过Proxy类的静态方法获得代理类:
Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),new Class<?>[] { Foo.class },handler);
获取类加载器,哪个类的都可以, 一般是使用当前类
被代理类所对应接口的Class对象数组,可以多个
实现InvocationHandler的类实例

第二个接口实现类:

public class InterfaceBaseReal2 implements InterfaceBase{
public void proxy() {
System.out.println("InterfaceBase22222......");
}
}

处理调用程序:

//实现InvacationHandler接口的类,这个类被成为调用处理程序
public class BaseInvocationHandler implements InvocationHandler {
//invocationHandler实现类里面必须传入被代理的类(实现被代理接口的类)
//这里使用对象的顶层基类,主要可以传入不同的对象
private Object object;
//通过构造函数传入
public BaseInvocationHandler(Object object) {
this.object = object;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("InterfaceBaseRealProxy....before");
//调用被代理类的功能,保留原来的不变
Object re =method.invoke(object,args);
System.out.println("InterfaceBaseRealProxy....after");
//返回真是代理对象,也可以通过 return proxy
return re;
}
}

获取代理实例:

public class HandlerTest {
public static void main(String[] args) {
//被代理类所实现的接口的Class对象
Class[]inter = {InterfaceBase.class};
//被代理的类的实例
InterfaceBase interfaceBase = new InterfaceBaseReal();
//实现接口InvocationHandler的类的实例(必须传入被代理的类)
BaseInvocationHandler bh = new BaseInvocationHandler(interfaceBase);
//通过Proxy的静态函数得到代理实例
InterfaceBase ibproxy = (InterfaceBase) Proxy.newProxyInstance(InterfaceBase.class.getClassLoader(),inter,bh);
ibproxy.proxy(); System.out.println(ibproxy.getClass());
System.out.println("2222222222222222222222222222222222");
//第二个被代理的类
InterfaceBase interfaceBase2 = new InterfaceBaseReal2();
BaseInvocationHandler bh2 = new BaseInvocationHandler(interfaceBase2);
InterfaceBase ibproxy2 = (InterfaceBase)Proxy.newProxyInstance(HandlerTest.class.getClassLoader(),inter,bh2);
ibproxy2.proxy();
}
}

注意:因为我们的处理调用程序当中设置传入的对象为Object,所以可以传入不同的对象

最新文章

  1. java内置工具
  2. mongodb(副本集)
  3. POJ 2480 求每一个数对于n的最大公约数的和
  4. android学习笔记22——Notification
  5. 剑指offer习题集
  6. LeetCode40 Combination Sum II
  7. Java SE ---关系运算符
  8. Android的Handler几种常见的传值方式
  9. 视频边下边播--缓存播放数据流-b
  10. 模板--&gt;Matrix重载运算符:+,-,x
  11. 在chrome中使用axure生成原型的问题
  12. ubuntu 下安装 matplotlib
  13. Tungsten Replicator学习总结
  14. 基于c编写的关于随机生成四则运算的小程序
  15. 前端---js02
  16. Cordova打包Apk
  17. php几种常见排序算法
  18. spring cloud: zuul(五): prefix访问前缀, ignoredServices粗粒度访问, yml不起作用
  19. Flutter at Google I/O 2018
  20. kafka学习总结之集群部署和zookeeper

热门文章

  1. 我哭了 看到RCNN 和FAST RCNN时 想着为什么要选候选框呢?能不能直接回归出来候选框?
  2. 三大数据库 sequence 之华山论剑 (上篇)
  3. 微服务从代码到k8s部署应有尽有系列(九、事务精讲)
  4. C#的泛型和Java的伪泛型
  5. csv/json/list/datatable导出为excel的通用模块设计
  6. Pandas:plot相关函数
  7. 02_opencv_python_图像处理进阶
  8. 我的hacker标杆
  9. mysql中创建函数时报错信息
  10. (七)React Ant Design Pro + .Net5 WebApi:后端环境搭建-日志、异常处理