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

使用反射生成JDK动态代理

2013-05-21 16:20 483 查看
在java.lang.reflect包下提供了一个Proxy类和一个InvocationHandler接口,通过这个类和接口可以生成JDK动态代理类或者动态代理对象。

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

static Class<?>
getProxyClass(ClassLoader loader,

Class<?>... interfaces)
static Object


, java.lang.reflect.InvocationHandler)]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:代表调用目标方法时传入的实参
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: