1.Proxy.newProxyInstance(source.getClass().getClassLoader(), source.getClass().getInterfaces(), myInvoHandler)
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
class MyInvocationHandler implements InvocationHandler{
private Object source ;
public MyInvocationHandler(Object source){
this.source = source ;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// System.out.println(proxy);// StackOverflowError course by recurse
System.out.println(proxy.getClass());//proxy-class,not source-class
if(null!=args){
for(Object param : args){
System.out.println(param);//method-input-param
}
}
System.out.println("u can do special things before method-execute");
Object obj = method.invoke(source, args);//Point:source,not proxy. proxy's meaning?
System.out.println("u can do special things after method-execute");
return obj ;
}
}
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ClassProxyTest {
/**
* @param args
*/
public static void main(String[] args) {
PojoWithoutInterface pojo = new PojoWithoutInterface();
pojo.setName(1111);
Object pojoProxy = ClassProxyTest.getProxyInstance(pojo);
// pojo = (PojoWithoutInterface) pojoProxy ;// class cast exception
System.out.println(pojoProxy);
Object pojoProxy1 = ClassProxyTest.getProxyInstance(pojo);// not equals,new instance
System.out.println(pojoProxy.equals(pojoProxy1));//object 's method should be proxy
ClassImplInterface impl = new ClassImplInterface() ;
Object implProxy = ClassProxyTest.getProxyInstance(impl) ;
InterfaceA interA = (InterfaceA) implProxy ;
interA.methodA() ;
}
public static Object getProxyInstance(Object source){
InvocationHandler myInvoHandler = new MyInvocationHandler(source) ;
Object proxy = Proxy.newProxyInstance(source.getClass().getClassLoader(), source.getClass().getInterfaces(), myInvoHandler) ;
Method[] publicMethods = proxy.getClass().getMethods() ;// proxy only have source's interface-method,not all-method
if(null!=publicMethods){
for(Method mthd : publicMethods){
System.out.println(mthd.getName());
}
}
return proxy ;
}
}
class ClassImplInterface implements InterfaceA,InterfaceB{
@Override
public void methodA() {
System.out.println("methodA execute");
}
@Override
public void methodB() {
System.out.println("methodB execute");
}
public void methodC(){//proxy can not execute
System.out.println("self-method execute");
}
}
public class PojoWithoutInterface {
private int name ;
public int getName() {
return name;
}
public void setName(int name) {
this.name = name;
}
}
public interface InterfaceA {
void methodA() ;
}
public interface InterfaceB {
void methodB() ;
}
分享到:
相关推荐
public class proxy { public static void main(String[] args) throws Exception { HttpHost entry = new HttpHost("43.248.99.36", 62084); String resp= Executor.newInstance() .auth(entry, "fd", "123")//...
例如Class<?> classType = Class.forName(java.lang.String); 例子: a..使用?通配符可以引用其他各种参数化的类型,但不能调用与参数化有关的方法; Collection<?> c = new Vector(); c.add("abc");//报错 c.size...
之前我已经写过了关于动态代理的两篇文章,本来以为这块应该没啥问题,没...public final class proxy extends Proxy implements MyService { private static Method m1; private static Method m4; private stati
代理模式(proxy)为其他对象提供一个代理以控制对这个对象的访问。结构图//Subject类,定义了RealSubjext和Proxy的公共接口,这样就在任何使用RealSubject...//Proxy类,保存一个引用使得代理可以访问实体classProxy:pu
Java动态代理内存中生成的$Proxy0类
public class Client { public static void main(String[] args) { // UserService userService=new UserServiceImpl(); // ProxyInovationHandler pih =new ProxyInovationHandler(); // pih.setTarget...
资源分类:Python库 所属语言:Python 资源全名:proxy-class-0.0.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
class proxy { /* 需采集列表 */ public $list; /* 代理列表 保存路径 */ public $save_path = 'proxy.txt'; /* 获取采集列表 */ function get_list($page) { $url = 'http://www.cnproxy.com/proxy(*)....
代理模式 Proxy模式是一种常用的设计模式,它主要...class Proxy(object): def __init__(self, subject): self.__subject = subject # 代理其实本质上就是属性的委托 def __getattr__(self, name): return geta
代理模式 Proxy模式是一种常用的设计模式,它主要用来通过一个...class Proxy(object): def __init__(self, subject): self.__subject = subject # 代理其实本质上就是属性的委托 def __getattr__(self, name): r
public class ProxyUpTest { public static void main(String[] args) { UserMgr userMgr = new UserMgrImpl(); Object proxy = ProxyUp.newProxy(userMgr, new MethodInterceptor(){ public void invoke...
如果你前面4篇文章都没落下,那我想你一定会有兴趣看完下面所有的代码,并且会对proxy的实现和class字节码有更深刻的理解 当然,如果你看到源码就非常头疼也没有关系,可以跳过这部分源码直接看最后的验证部分
如果使用LinFu.DynamicProxy动态代理,引用NHibernate.ByteCode.LinFu.dll程序集并配置proxyfactory.factory_class节点为 <property name="proxyfactory.factory_class"> NHibernate.ByteCode.LinFu....
cas 配置client 1.0 &2.0 及proxy DEMO 说明 1 cas server 搭建 1.1 资源准备 cas server 下载 http://www.ja-sig.org/downloads/cas/cas-server-3.3.1-release.zip 1.2 解压后打开cas-server-3.3.1-release\cas-...
代理刮板用于抓取用PHP编写的免费代理列表的库快速开始composer require vantoozz/proxy-scraper: ~ 3 guzzlehttp/guzzle: ~ 7 guzzlehttp/psr7 hanneskod/classtools <?php declare (strict_types= 1 );use ...
骨干代理 Backbone 的模型代理。 在具有不同关注点的多个组件... Instances will proxy the given user model // Note that user is a model _instance_ - not a class var UserProxy = BackboneProxy . extend ( us
代理模式典型的结构图为: 实际上,代理模式的想法非常简单。 代理模式的实现: ...class Subject{ public: virtual ~Subject(); virtual void Request() = 0; protected: Subject(); private:
最新版本合并到 hf_ctp_py_proxy 项目中, hf_ctp_cs_proxy 上期技术期货交易api之C#封装,实现接口调用。支持winform数据绑定即时更新。... public class TestQuote { CTPQuote _q = null; string _
ws_proxya simple webservice message proxy system该项目提供对webservice消息的路由功能,因为只是实现高效转发,不需要提供具体服务,所以关键点就是如何不解析soap消息,而只找到其路由的关键key,然后对其进行...
所谓Dynamic Proxy是这样一种class:它是在运行时生成的class,在生成它时你必须提供一组interface给它,然后该class就宣称它实现了这些 interface。你当然可以把该class的实例当作这些interface中的任何一个来用。...