Proxy动态代理
Proxy动态代理
package com.test.dynamicproxy;
public interface Subject {
public void request();
}
package com.test.dynamicproxy;
public class RealSubject implements Subject {
@Override
public void request() {
System.out.println("request() from RealSubject");
}
}
package com.test.dynamicproxy; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; public class SubjectInvocationHandler implements InvocationHandler { private Object object; public SubjectInvocationHandler(Object obj) {
this.object = obj;
} @Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
method.invoke(object,args);
return null;
} }
package com.test.dynamicproxy; import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy; public class Client { public static void main(String[] args) { RealSubject real = new RealSubject();
InvocationHandler ih = new SubjectInvocationHandler(real);
Class<?> clazz = real.getClass();
Subject s = (Subject)Proxy.newProxyInstance(clazz.getClassLoader(),clazz.getInterfaces(),ih); s.request(); //request() from RealSubject System.out.println(s.getClass().getName()); //$Proxy0
System.out.println(s instanceof Proxy); //true for(Class interf : s.getClass().getInterfaces()){
System.out.println(interf.getName()); //com.test.dynamicproxy.Subject
} // System.out.println(s.getClass().getPackage()); //null
// System.out.println(s.getClass().getSuperclass()); //class java.lang.reflect.Proxy
// System.out.println(Proxy.class.getPackage()); //package java.lang.reflect, Java Platform API Specification, version 1.6
} }
Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler. This method is equivalent to:
Proxy.getProxyClass(loader, interfaces).
getConstructor(new Class[] { InvocationHandler.class }).
newInstance(new Object[] { handler });
Proxy.newProxyInstance throws IllegalArgumentException for the same reasons that Proxy.getProxyClass does.
- Parameters:
- loader the class loader to define the proxy class
- interfaces the list of interfaces for the proxy class to implement
- h the invocation handler to dispatch method invocations to
- Returns:
- a proxy instance with the specified invocation handler of a proxy class that is defined by the specified class loader and that implements the specified interfaces
- Throws:
- IllegalArgumentException - if any of the restrictions on the parameters that may be passed to
getProxyClassare violated - NullPointerException - if the
interfacesarray argument or any of its elements arenull, or
Client 中 s extends Proxy implements Subject
s.request() 方法调用流程 :
s 是 Subject 的 implementation .s.request() 的实现
s 伪代码:
s extends Proxy implements Subject{
InvocationHandler ih;
......
public void request(){
ih.invoke(....);
}
}
Proxy动态代理的更多相关文章
- Proxy 动态代理 InvocationHandler CGLIB MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 动态代理案例1:运用Proxy动态代理来增强方法
动态代理案例1: /*要求:运用Proxy动态代理来增强方法 题目: 1.定义接口Fruit,其中有addFruit方法 2.定义实现类FruitImpl,实现Fruit接口 3.定 ...
- JAVA的Proxy动态代理在自动化测试中的应用
JAVA的动态代理,在MYBATIS中应用的很广,其核心就是写一个interface,但不写实现类,然后用动态代理来实例化并执行这个interface中的方法,话不多说,来看一个实现的例子: 1.先定 ...
- 利用JDK的中Proxy动态代理实现Spring的AOP技术
首先给出设计模式静态代理与动态代理的学习: http://layznet.iteye.com/blog/1182924 讲的不错 然后我们实现AOP 就要求我们对委托的所有方法的调用实现拦截 代理 ...
- 【Java深入研究】5、Proxy动态代理机制详解
在学习Spring的时候,我们知道Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,依赖注入就不用多说了,而对于Spring的核心AOP来说,我们不但要知道怎么通过AOP来满足的 ...
- Dynamic Proxy (动态代理模式)
动态代理(运行期行为)主要有一个 Proxy类 和一个 InvocationHandler接口 动态代理角色: 1. 抽象主题角色 2. 真实主题角色(实现了抽象主题接口) 3. 动态代理主题角色(实 ...
- Proxy动态代理-增强方法
增强对象的功能 设计模式:一些通用的解决固定问题的方式 装饰器模式 代理模式 概念: 在代理模式(Proxy Pattern)中,一个类代表另一个类的功能.这种类型的设计模式属于结构型模式. 在代理模 ...
- Java进阶 | Proxy动态代理机制详解
一.Jvm加载对象 在说Java动态代理之前,还是要说一下Jvm加载对象的过程,这个依旧是理解动态代理的基础性原理: Java类即源代码程序.java类型文件,经过编译器编译之后就被转换成字节代码.c ...
- Java设计模式—Proxy动态代理模式
代理:设计模式 代理是一种常用的设计模式,其目的就是为其他对象提供一个代理以控制对某个对象的访问.代理类负责为委托类预处理消息,过滤消息并转发消息,以及进行消息被委托类执行后的后续处理. 图 1. 代 ...
随机推荐
- CodeIgniter框架——知识要点汇总
NO1.学习要点: 一.CodeIgniter 框架的简介 二.CodeIgniter 框架的安装 三.CodeIgniter 框架的目录结构分析 四.CodeIgniter 框架是如何工作的? 五. ...
- (转)gethostbyname() -- 用域名或主机名获取IP地址
struct hostent *gethostbyname(const char *name); 这个函数的传入值是域名或者主机名,例如"www.google.cn"等等.传出值, ...
- TGraphicControl(自绘就2步,直接自绘自己,不需要调用VCL框架提供的函数重绘所有子控件,也不需要自己来提供PaintWindow函数让管理框架来调用)与TControl关键属性方法速记(Repaint要求父控件执行详细代码来重绘自己,还是直接要求Invalidate无效后Update刷新父控件,就看透明不透明这个属性,因为计算显示的区域有所不同)
TGraphicControl = class(TControl) private FCanvas: TCanvas; procedure WMPaint(var Message: TWMPaint) ...
- css选择器的权重
权重会叠加!
- Python Selenium 自动化测试
本文转载 作者:灰蓝蓝蓝蓝蓝蓝链接:http://www.jianshu.com/p/5188cb3ab790來源:简书著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.更多技术博客 ...
- Python中的默认参数(转)
add by zhj: Python设计者为何将默认参数设计成这样呢?参见Python函数参数默认值的陷阱和原理深究 原文:https://github.com/acmerfight/insight_ ...
- KindEditor 4.1.11最新版 网站实例 包含使用方法 下载地址
KindEditor是非常好的富媒体文本编辑系统,短小精悍,可惜网上找到的都是有各种问题!一怒之下,我自己进行了修改和配置,搞成了一个网站示例.下载解压后,可以直接运行!!完全无问题.! Kinded ...
- LeetCode-day04
35. Same Tree 判断两棵树相同 36. Invert Binary Tree 翻转树 37. Symmetric Tree 判断树是不是镜像对称的 38. Maximum Depth of ...
- iframe 跨域请求
iframe.contentWindow 兼容各个浏览器,可取得子窗口的 window 对象. iframe.contentDocument Firefox 支持,> ie8 的ie支持.可取得 ...
- smarty变量调节器与函数
smarty自带了一些变量调节器与内置函数,都在libs/plugins目录下,变量调节器以modifier开头,函数以function开头,而且我们可以自定义变量调节器与函数,熟练运用之后会极大地提 ...