在java.lang.reflect包下提供了一个Proxy类和一个InvocationHandler接口,通过这个类和接口可以生成JDK动态代理类或者动态代理对象。
Proxy提供了两个方法创建动态代理类和动态代理对象:
static Class<?> | getProxyClass(ClassLoader loader, Class<?>... interfaces) |
|
|
第一种:大家都会
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;
}
}
对于第二种方法:就是分步骤创建代理类:
。
*******************
public Object invoke(Object proxy, Method method, Object[] args)
三个 参数解释:
proxy:代表动态代理对象
method:代表正在执行的方法
args:代表调用目标方法时传入的实参