JDK的动态代理
* 1、产生的代理类与接口是什么关系? 代理类实现了接口
* 2、代理类的方法体的内容是什么? 方法体的内容就是中invoke方法体的内容 * 3、的作用是什么? * 4、中的invoke方法是什么?
完成invoke方法体中的内容
客户端调用哪个方法,method就代理哪个方法
是代理类的方法
* 4、中的invoke方法的参数method
实现了InvocationHandler接口 public MyInterceptor(Object target, Transaction transaction) { super(); this.target = target; this.transaction = transaction; } /** * @param proxy 代理类对象
* @param method 被代理的接口方法 * @param args 被代理接口方法的参数 * @return 方法调用返回的结果 */ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { this.transaction.beginTransaction(); method.invoke(this.target, args); this.transaction.commit(); return null; } 代理类
public void testJDKProxy(){ Object target = new PersonDaoImpl(); Transaction transaction = new Transaction(); MyInterceptor interceptor = new MyInterceptor(target, transaction);
//产生代理对象
/**
* 第一个参数 * 第二个参数 * 第三个参数 */
类加载器
目标类实现的所有的接口
PersonDao personDao = (PersonDao)Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), interceptor); personDao.updatePerson();