您的位置:首页 > 编程语言 > Java开发

Java动态代理实现

2016-04-07 14:39 477 查看
一般情况,代理模式中的接口和代理类都被固定了,这种模式称为静态代理。

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