关于动态代理的初步认识
2015-09-05 15:46
316 查看
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
class MyInvocationHandler implements InvocationHandler{
private Object obj;
public Object bind(Object obj){
this.obj=obj;
return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),this);//根据前两个参数真实主题实现类的类加载器和所有父类接口创建Proxy类的一个子类并实例化,this是当前MyInvocationHandler实例对象,用于接下来的$Proxy0实例对象调用其invoke()方法。
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
before();
Object temp=method.invoke(this.obj, args);
return temp;
}
public void before(){
System.out.println("before");
}
}
interface Subject{
public String say(String name,int age);
}
class RealSubject implements Subject{
@Override
public String say(String name, int age) {
// TODO Auto-generated method stub
return "姓名"+name+"年龄"+age;
}
}
public class DynaProxyDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyInvocationHandler my=new MyInvocationHandler();
Subject sub=(Subject) my.bind(new RealSubject());//这里实际上是创建了一个代理类实例$Proxy0,该实例也实现了了obj对象的所有接口
System.out.println(sub.say("joe", 11));;//这里调用的其实是$Proxy0覆写过的say()方法,在该方法中,默认调用MyInvocationHandler类的实例对象my的invoke()方法
Fruit f=(Fruit) my.bind(new Apple());
f.eat();
}
}
参考:http://blog.csdn.net/zcc_0015/article/details/22695647
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
class MyInvocationHandler implements InvocationHandler{
private Object obj;
public Object bind(Object obj){
this.obj=obj;
return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),this);//根据前两个参数真实主题实现类的类加载器和所有父类接口创建Proxy类的一个子类并实例化,this是当前MyInvocationHandler实例对象,用于接下来的$Proxy0实例对象调用其invoke()方法。
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
// TODO Auto-generated method stub
before();
Object temp=method.invoke(this.obj, args);
return temp;
}
public void before(){
System.out.println("before");
}
}
interface Subject{
public String say(String name,int age);
}
class RealSubject implements Subject{
@Override
public String say(String name, int age) {
// TODO Auto-generated method stub
return "姓名"+name+"年龄"+age;
}
}
public class DynaProxyDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyInvocationHandler my=new MyInvocationHandler();
Subject sub=(Subject) my.bind(new RealSubject());//这里实际上是创建了一个代理类实例$Proxy0,该实例也实现了了obj对象的所有接口
System.out.println(sub.say("joe", 11));;//这里调用的其实是$Proxy0覆写过的say()方法,在该方法中,默认调用MyInvocationHandler类的实例对象my的invoke()方法
Fruit f=(Fruit) my.bind(new Apple());
f.eat();
}
}
参考:http://blog.csdn.net/zcc_0015/article/details/22695647
相关文章推荐
- c++のeof
- sharepoint 2016 学习系列篇(1)-关于sharepoint
- POJ1039Pipe【线段相交判断+求交点】
- Erdas的Model Maker功能
- AngularJS 应用身份认证的技巧
- 照明
- 数据结构--二叉树遍历
- js图片延迟加载如何实现
- hdu 5239__Doom
- Linux进程间通信——使用信号量
- poj-1066 Treasure Hunt
- N皇后问题
- 欧拉工程第64题:Odd period square roots
- option selected index
- PHP学习(一)--认识PHP
- php yii框架 目录说明
- NOIP Mayan游戏
- 2.2.3 Order-Independent Transparency (about order-independent blending) 顺序无关透明度(关于顺序无关的混合)
- leetcode[242] Valid Anagram
- Arcgis for javascript实现百度地图ABCD marker的效果