Java动态代理实现
2016-04-07 14:39
477 查看
一般情况,代理模式中的接口和代理类都被固定了,这种模式称为静态代理。
JDK1.3引入了动态代理,写个实例比较清楚。
JDK1.3引入了动态代理,写个实例比较清楚。
public interface UserDao { public Boolean findUserById(String userId); } public class UserDaoImpl implements UserDao { @Override public Boolean findUserById(String userId) { if (userId.equalsIgnoreCase("userId001")) { System.out.println("查询ID为" + userId + "的用户信息成功!"); return true; } else { System.out.println("查询ID为" + userId + "的用户信息失败!"); return false; } } } public interface DocumentDao { public Boolean deleteDocumentById(String documentId); } public class DocumentDaoImpl implements DocumentDao { @Override public Boolean deleteDocumentById(String documentId) { if (documentId.equalsIgnoreCase("id001")) { System.out.println("删除ID为" + documentId + "的文档信息成功!"); return true; } else { System.out.println("删除ID为" + documentId + "的文档信息失败!"); return false; } } } import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class LogHandler implements InvocationHandler { private Object obj; public LogHandler(Object obj) { this.obj = obj; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("调用时间:"+ System.currentTimeMillis()); System.out.println("method:"+ method); Object result = method.invoke(obj, args); System.out.println("返回结果:"+ result); System.out.println("结束时间:"+ System.currentTimeMillis()); return result; } } import java.lang.reflect.Constructor; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; public class Test { public static void main(String[] args) throws Exception { // 简化了下面的2-4步 InvocationHandler handler = new LogHandler(new UserDaoImpl()); UserDao userDaoProxy = (UserDao) Proxy.newProxyInstance(LogHandler.class.getClassLoader(), new Class[]{UserDao.class}, handler); System.out.println(userDaoProxy.getClass().getName()); userDaoProxy.findUserById("userId001"); System.out.println("---------------------------------"); handler = new LogHandler(new DocumentDaoImpl()); DocumentDao documentDaoProxy = (DocumentDao) Proxy.newProxyInstance(LogHandler.class.getClassLoader(), new Class[]{DocumentDao.class}, handler); System.out.println(documentDaoProxy.getClass().getName()); documentDaoProxy.deleteDocumentById("id001"); System.out.println("---------------------------------"); // InvocationHandlerImpl 实现了 InvocationHandler 接口,并能实现方法调用从代理类到委托类的分派转发 // 其内部通常包含指向委托类实例的引用,用于真正执行分派转发过来的方法调用 handler = new LogHandler(new UserDaoImpl()); // 通过 Proxy 为包括 Interface 接口在内的一组接口动态创建代理类的类对象 Class<?> clazz = Proxy.getProxyClass(LogHandler.class.getClassLoader(), new Class[] { UserDao.class }); // 通过反射从生成的类对象获得构造函数对象 Constructor<?> constructor = clazz.getConstructor(new Class[] { InvocationHandler.class }); // 通过构造函数对象创建动态代理类实例 UserDao proxy = (UserDao) constructor.newInstance(new Object[] { handler }); System.out.println(proxy.getClass().getName()); proxy.findUserById("userId001"); } }
控制台输出结果: $Proxy0 调用时间:1460016794972 method:public abstract java.lang.Boolean UserDao.findUserById(java.lang.String) 查询ID为userId001的用户信息成功! 返回结果:true 结束时间:1460016794972 --------------------------------- $Proxy1 调用时间:1460016794975 method:public abstract java.lang.Boolean DocumentDao.deleteDocumentById(java.lang.String) 删除ID为id001的文档信息成功! 返回结果:true 结束时间:1460016794975 --------------------------------- $Proxy0 调用时间:1460016794975 method:public abstract java.lang.Boolean UserDao.findUserById(java.lang.String) 查询ID为userId001的用户信息成功! 返回结果:true 结束时间:1460016794975
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树