第十四章 类型信息4.0

[color=blue]动态代理[/color]

Java的动态代理比代理的思想更向前迈进了一步,因为它可以动态地创建代理并动态地处理对所代理方法的调用。在动态代理上所做的所有调用都会被重定向到单一的调用处理器上,它的工作是揭示调用的类型并确定相应的对策。下面是代码:

1.被代理类 的接口 Proxied
// 被代理类 需实现的 接口
public interface Proxied {
void doSomething();
void doSomethingElse(String str);
}

2.一个 Proxied接口 的实现类(被代理类)
public class ConcreteProxied implements Proxied {
@Override
public void doSomething() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.err.println("Error : InterruptedException");
}
System.out.println(this.getClass().getSimpleName()
+ " >> doSomething .");
}

@Override
public void doSomethingElse(String str) {
try {
Thread.sleep(150);
} catch (InterruptedException e) {
System.err.println("Error : InterruptedException");
}
System.out.println(this.getClass().getSimpleName()
+ " >> doSomethingElse , argument = " + str + ".");
}
}

3,TimingInvocationHandler 类,实现了 InvocationHandler 接口
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class TimeingInvocationHandler implements InvocationHandler{
//被代理的对象
private Object proxied;
public TimeingInvocationHandler(Object proxied){
this.proxied = proxied;
}

// 参数 proxy 表示代理类的对象
// 参数 method 表示被代理类 和 代理类 都实现的接口 的方法对象
// 参数 args 表示方法 method 的参数数组
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println(method.getDeclaringClass().getName());
long currentTimeMillis = System.currentTimeMillis();
Object ret = method.invoke(proxied, args); //被代理对象的方法
System.out.println(this.getClass().getSimpleName()+" >> wastes time : "
+(System.currentTimeMillis() - currentTimeMillis)+"ms");
return ret;
}

}

4,测试类 Test
import java.lang.reflect.Proxy;

public class TestProxy {
public static void main(String[] args) {
Proxied proxied = new ConcreteProxied();
proxied.doSomething();
proxied.doSomethingElse("only a String");

// 生成一个代理实例,这个代理实现了 Proxied 接口
// 对这个代理(proxy)的方法的调用 会 重定向到 TimeingInvocationHandler 的 invoke 方法
Proxied proxy = (Proxied) Proxy.newProxyInstance(Proxied.class
.getClassLoader(), // 类加载器
new Class[] { Proxied.class }, // 代理要实现的接口列表
new TimeingInvocationHandler(proxied) // 调用处理器
);
proxy.doSomething();
proxy.doSomethingElse("only a String");
}

}

运行Test类,输出如下:

ConcreteProxied >> doSomething .
ConcreteProxied >> doSomethingElse , argument = only a String.
Proxied
ConcreteProxied >> doSomething .
TimeingInvocationHandler >> wastes time : 110ms
Proxied
ConcreteProxied >> doSomethingElse , argument = only a String.
TimeingInvocationHandler >> wastes time : 140ms

通过调用静态方法Proxy.newProxyInstance()可以创建动态代理,[color=red]这个方法需要得到一个类加载器,一个你希望该代理实现的接口列表(不是类或抽象类),以及InvacationHandler接口的一个实现。动态代理可以将所有调用重定向到调用处理器,[/color]因此通常会向调用处理器的构造器传递给一个“实际”对象的引用,从而使得调用处理器在执行其中介任务时,可以将请求转发。

Invoke()方法中传递进来了代理对象,以防你需要区分请求的来源,但是在许多情况下,你并不关心这一点。然而,在invoke()内部,在代理上调用方法时需要格外当心,因为对接口的调用将被重定向为对代理的调用。

通常,你会行被代理的操作,然后使用Method.invoke()将请求转发给被代理对象,并传入必须的参数。这初看起来可能有些限制,就像你只能执行泛化操作一样,但是,你可以通过传递的参数,来过滤某些方法。
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if(method.getName().equals("interesting")) {
System.out.println("interesting");
}
}

Test类的:
proxy.doSomething();
proxy.interesting("bonobo");
proxy.doSomethingElse("only a String");


[color=blue]接口与类型信息[/color]
Method g = a.getClass().getDeClareMethod(methodName);
g.setAccessible(true);
g.invoke(a);

通过使用反射,仍旧可以到达并调用所有方法,甚至是private方法!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值