一、JDK的动态代理应用
创建一个借口
package jdkProxy;
public interface Moveable {
void move();
}
package jdkProxy;
public class car implements Moveable {
public void move(){
System.out.println("正在移动-----");
}
}
创建handler
package jdkProxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class TimerHandler implements InvocationHandler {
private Object target;
public TimerHandler(Object target) {
super();
this.target = target;
}
/**
* 被代理的对象 Object
* 被代理对象的方法 Method
* 被代理对象方法的参数 Object[]
*
*/
@Override
public Object invoke(Object arg0, Method arg1, Object[] arg2) throws Throwable {
// TODO Auto-generated method stub
System.out.println("开始");
arg1.invoke(target, arg2);
System.out.println("结束");
return null;
}
}
测试
package jdkProxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
public class Test {
public static void main(String[] args) {
car c = new car();
InvocationHandler h = new TimerHandler(c);
Class<?> cls = c.getClass();
/**
* 类加载器
* 实现接口
* handler
*/
Moveable m = (Moveable)Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), h);
m.move();
}
}
二、模拟jdk动态代理的实现
1、创建的接口Moveable和接口的实现类Car和上面的代码一样
2、自己手动定义InvocationHandler接口
package jdkProxy;
import java.lang.reflect.Method;
public interface InvocationHandler{
public void invoke(Object o, Method m) throws Exception;
}
3、创建TimerHandler
创建handler
package jdkProxy;
//import java.lang.reflect.InvocationHandler;注意这样地方的InvocationHandler一定要引用自己手写的
import java.lang.reflect.Method;
public class TimerHandler implements InvocationHandler {
private Object target;
public TimerHandler(Object target) {
super();
this.target = target;
}
/**
* 被代理的对象 Object
* 被代理对象的方法 Method
*/
@Override
public Object invoke(Object arg0, Method arg1) throws Throwable {
// TODO Auto-generated method stub
System.out.println("开始");
arg1.invoke(target);
System.out.println("结束");
return null;
}
}
4、编写代理类
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import javax.tools.JavaCompiler.CompilationTask;
import org.apache.commons.io.FileUtils;
public class Proxy {
@SuppressWarnings("unchecked")
public static Object newProxyInstance(Class infce,InvocationHandler h) throws Exception{
String rt = "\r\n";
String methodStr = "";
for(Method m : infce.getMethods()){
methodStr += " @Override" + rt +
" public void " + m.getName() + "() {" + rt +
" try{" + rt +
" Method md = " + infce.getName() + ".class.getMethod(\""
+ m.getName() + "\");" + rt +
" h.invoke(this,md);" +rt+
" }catch(Exception e){ e.printStackTrace();}" + rt +
" }" ;
}
String str =
"package jdkProxy;" + rt +
"import java.lang.reflect.Method;" + rt +
"import jdkProxy.InvocationHandler;" + rt+
"public class $Proxy0 implements " + infce.getName() + " {" + rt +
" public $Proxy0(InvocationHandler h) {" + rt +
" this.h = h;" + rt +
" }" + rt +
" private InvocationHandler h;" + rt+
methodStr + rt +
"}" ;
//产生代理类的java文件
String filename = System.getProperty("user.dir") +"/jdkProxy/$Proxy0.java";
File file = new File(filename);
FileUtils.writeStringToFile(file, str);
//编译
//拿到编译器
JavaCompiler complier = ToolProvider.getSystemJavaCompiler();
//文件管理者
StandardJavaFileManager fileMgr =
complier.getStandardFileManager(null, null, null);
//获取文件
Iterable units = fileMgr.getJavaFileObjects(filename);
//编译任务
CompilationTask t = complier.getTask(null, fileMgr, null, null, null, units);
//进行编译
t.call();
fileMgr.close();
//load 到内存
ClassLoader cl = ClassLoader.getSystemClassLoader();
Class c = cl.loadClass("jdkProxy.$Proxy0");
Constructor ctr = c.getConstructor(InvocationHandler.class);
return ctr.newInstance(h);
}
}
6、测试
public class Test {
public static void main(String[] args) throws Exception{
Car c = new Car();
InvocationHandler h = new TimerHanndler(c);
Moveable m = (Moveable) Proxy.newProxyInstance(Moveable.class,h);
m.stop();
}
}
初学咋练,多多指教