使用反射生成JDK动态代理
2013-05-21 16:20
483 查看
在java.lang.reflect包下提供了一个Proxy类和一个InvocationHandler接口,通过这个类和接口可以生成JDK动态代理类或者动态代理对象。
Proxy提供了两个方法创建动态代理类和动态代理对象:
第一种:大家都会
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:代表调用目标方法时传入的实参
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:代表调用目标方法时传入的实参
相关文章推荐
- Java 使用反射生成JDK动态代理
- 使用反射生成JDK动态代理---使用Proxy和InvocationHandler创建动态代理
- java使用反射生成JDK动态代理
- 使用反射生成JDK动态代理---动态代理和AOP
- 使用反射生成jdk动态代理
- 类加载机制与反射5——使用反射生成JDK动态代理
- 反射 生成JDK动态代理和AOP
- jdk动态代理(动态生成字节码与反射机制的结合)
- java反射——运用反射生成jdk动态代理
- 27 API-反射(类的加载器,反射的使用,动态代理)&设计模式(装饰设计模式,模版设计模式)&JDK新特性(JDK5,JDK6,JDK7,DK8)
- Java反射—运用反射生成jdk动态代理
- Java反射之JDK动态代理实现简单AOP
- Java JDK动态代理(AOP)的实现原理与使用详析
- 动态代码的使用(反射和动态生成类)(转载)
- 反射与动态代理的应用(一):在RPC中的使用
- 使用反射增加装饰模式的普适性,动态代理实现装饰模式
- 【Java EE 学习 24 下】【注解在数据库开发中的使用】【反射+注解+动态代理在事务中的应用service层】
- JAVA JDK的动态代理反射实现
- Java JDK 动态代理(AOP)使用及实现原理分析
- JDK核心API:使用动态代理实现AOP功能