使用反射生成JDK动态代理

本文介绍Java中使用JDK动态代理创建代理对象的两种方法。一种是直接利用newProxyInstance方法,另一种则是通过分步创建代理类的方式。文章还详细解释了InvocationHandler接口的作用及其invoke方法的参数含义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在java.lang.reflect包下提供了一个Proxy类和一个InvocationHandler接口,通过这个类和接口可以生成JDK动态代理类或者动态代理对象。

Proxy提供了两个方法创建动态代理类和动态代理对象:

static Class<?>getProxyClass(ClassLoader loader, Class<?>... interfaces)

static Object

newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)

 

第一种:大家都会

 

package prox;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest {

 private static final Class<?> Person = null;

 
 public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchMethodException {
  // TODO Auto-generated method stub


  // InvocationHandler
 Person person = (Person) Proxy.newProxyInstance(
    Person.class.getClassLoader(), new Class[] { Person.class },
    new MyInvocationHandler());
    
   person.walk();
  person.sayHello("ffff");
 }

}

interface Person {
 void walk();

 void sayHello(String name);
}

class MyInvocationHandler implements InvocationHandler {

 @Override
 public Object invoke(Object proxy, Method method, Object[] args)
   throws Throwable {
  // TODO Auto-generated method stub
  if (args != null) {
   System.out.println("下面是执行该方法时传入的实参为:");
   for (Object object : args) {
    System.out.println(object);
   }

  } else {
   System.out.println("该方法没有实参!!!");
  }
  return null;
 }
}

第二种:

package prox;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest {

 private static final Class<?> Person = null;

 
 public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchMethodException {
  // TODO Auto-generated method stub


   /*MyInvocationHandler mHandler =  new MyInvocationHandler();
  Class proxyClass =  Proxy.getProxyClass(Person.class.getClassLoader(), Person.class.getInterfaces());
  Constructor constructor = proxyClass.getConstructor(new Class[]{InvocationHandler.class});
  Person person  = (prox.Person) constructor.newInstance(new MyInvocationHandler());*/
  InvocationHandler handler = new MyInvocationHandler();
      Class proxyClass = Proxy.getProxyClass(Person.class.getClassLoader(), new Class[] { Person.class });
      Person person = (Person) proxyClass.getConstructor(new Class[] { InvocationHandler.class }).
          newInstance(new Object[] {handler});
  person.walk();
  person.sayHello("ffff");
 }

}

interface Person {
 void walk();

 void sayHello(String name);
}

class MyInvocationHandler implements InvocationHandler {

 @Override
 public Object invoke(Object proxy, Method method, Object[] args)
   throws Throwable {
  // TODO Auto-generated method stub
  if (args != null) {
   System.out.println("下面是执行该方法时传入的实参为:");
   for (Object object : args) {
    System.out.println(object);
   }

  } else {
   System.out.println("该方法没有实参!!!");
  }
  return null;
 }
}

 

对于第二种方法:就是分步骤创建代理类:

通过实现 InvocationHandler 接口创建自己的调用处理器;
通过为 Proxy 类指定 ClassLoader 对象和一组 interface 来创建动态代理类;
通过 反射机制获得动态代理类的 构造函数其唯一参数类型是调用处理器接口类型
通过 构造函数创建动态代理类实例,构造时调用处理器对象作为参数被传入。
 
在上述方法中采用注释部分,一直报错, 必须采用 new Class[] { Person.class }形式、 new Class[] { InvocationHandler.class }、
 。

*******************

 public Object invoke(Object proxy, Method method, Object[] args)

三个 参数解释:

proxy:代表动态代理对象

method:代表正在执行的方法

args:代表调用目标方法时传入的实参

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值