标签:
package com.sample.sping_ireport.cglib; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.Calendar; import java.util.GregorianCalendar; public class ProxyTest { public static void main(String[] args) { InvocationHandler handler = null;//调用代理 AbstractUserDao userDao = new UserDao();//DAO handler = new DaoLogHandler(userDao); AbstractUserDao proxy = null; //动态创建代理对象,用于代理一个AbstarctUserDao类型的真实主题对象 proxy = (AbstractUserDao) Proxy.newProxyInstance(AbstractUserDao.class.getClassLoader(), new Class[]{AbstractUserDao.class}, handler); proxy.findUserById("张无忌"); System.out.println("------------------------"); AbstractDocumentDao docDAO = new DocumentDao(); handler = new DaoLogHandler(docDAO); AbstractDocumentDao proxy_new = null; //动态创建代理对象,用于代理一个AbstractDocumentDAO类型的真实主题对象 proxy_new = (AbstractDocumentDao)Proxy.newProxyInstance(AbstractDocumentDao.class.getClassLoader(), new Class[]{AbstractDocumentDao.class}, handler); proxy_new.deleteDocumentById("D001"); //调用代理对象的业务方法 } } interface AbstractUserDao{ public Boolean findUserById(String userId); } interface AbstractDocumentDao{ public Boolean deleteDocumentById(String documentId); } class UserDao implements AbstractUserDao{ @Override public Boolean findUserById(String userId) { if(userId.equalsIgnoreCase("张无忌")){ System.out.println("查询ID为"+userId+"的信息成功"); return true; }else{ System.out.println("查询ID为"+userId+"的信息失败"); return false; } } } class DocumentDao implements AbstractDocumentDao{ @Override public Boolean deleteDocumentById(String documentId) { if(documentId.equalsIgnoreCase("D001")){ System.out.println("删除ID为"+documentId+"的文档成功"); return true; }else{ System.out.println("查询ID为"+documentId+"的文档失败"); return false; } } } //自定义请求处理类,用于实现代理 class DaoLogHandler implements InvocationHandler{ private Calendar calendar; private Object object; //带参和无参构造 public DaoLogHandler(){ } public DaoLogHandler(Object object){ this.object = object; } @Override public Object invoke(Object proxy, Method method, Object[] args)throws Throwable { beforeInvoke(); Object result = method.invoke(object, args); afterInvoke(); return result; } //调用之后 private void afterInvoke() { calendar = new GregorianCalendar(); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); int millSecond = calendar.get(Calendar.MILLISECOND); String time = hour + ":" +minute +":" +second +":" +millSecond; System.out.println("调用结束时间"+time); } //在调用之前 public void beforeInvoke() { calendar = new GregorianCalendar(); int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); int millSecond = calendar.get(Calendar.MILLISECOND); String time = hour + ":" +minute +":" +second+":" +millSecond; System.out.println("调用开始时间"+time); } } ----------------------------------- 代码执行结果: 调用开始时间10:38:2:902 查询ID为张无忌的信息成功 调用结束时间10:38:2:903 ------------------------ 调用开始时间10:38:2:904 删除ID为D001的文档成功 调用结束时间10:38:2:905
标签:
原文地址:http://www.cnblogs.com/bksqmy/p/4775097.html