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

Java中动态生成代理的方法

2016-05-31 15:53 615 查看
本文介绍Java 动态生成代理的方法

首先,说一下代理模式, 如下图,假设实际要使用的对象是Target实例,而Proxy是Target的代理, Proxy和Target必须要有相同的接口 TargetInterface, Client只依赖 TargetInterface,而不依赖具体的类。Client 对Proxy的方法调用,都被Proxy委托给Target。 



在这个模式里, 每个Target 类对应1个或多个 Proxy, 我们不光要写 Target类, 还要写它对应的Proxy类。 

而Java提供了一种动态生成代理的方法,我们只需要编写 Target 和 TargetInterface 不需要编写 Proxy类, Java可以动态给我们生成。

我们首先来编写TargetInterface  和 Target  类

package proxy;

public interface TargetInterface {
void say();
String getMsg();
}


package proxy;

public class Target implements TargetInterface {
@Override
public void say() {
System.out.println("Hello World!");
}

@Override
public String getMsg() {

return "messge from getMsg!";
}
}


现在我们为 Target 类动态生成一个代理, 在 Target 每个方法调用前和调用后,打出一些信息。

动态生成代理,需要用到java.lang.reflect.Proxy

AdviceInterface advice = new MyAdvice();

MyInvocationHandler myHandler = new MyInvocationHandler(target, advice);

Class<?>[] interfaces = { TargetInterface.class };

TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(TargetInterface.class.getClassLoader(),
interfaces, myHandler);


Proxy.newProxyInstance()方法需要传入一个 InvocationHandler 实例,Proxy对Targert的调用,由这个handler完成,InvocationHandler 是一个接口,这个接口只有一个方法需要实现, 下面是 MyInvocationHandler 代码:

package proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MyInvocationHandler implements InvocationHandler {
private Object target;
private AdviceInterface advice;

public MyInvocationHandler(Object target, AdviceInterface advice) {
this.target = target;
this.advice = advice;

}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
advice.before(target, method, args);
Object result = method.invoke(target, args);
advice.after(target, method, args);
return result;
}

}


上面代码中,把Target和 Advice 传入 InvocationHandler, Advice类封装了额外执行的代码。

Proxy把实际动作委派给 Target 的代码就是:

Object result = method.invoke(target, args);


这里还定义了一个AdviceInterface接口,用来抽象 Advice:

package proxy;

import java.lang.reflect.Method;

public interface AdviceInterface {
public void before(Object obj, Method method, Object[] args);
public void after(Object obj, Method method, Object[] args);
}


package proxy;

import java.lang.reflect.Method;

public class MyAdvice implements AdviceInterface{
public void before(Object obj, Method method, Object[] args){
System.out.println("MyAdvice - before method:" + method.getName() + " call");
}

public void after(Object obj, Method method, Object[] args) {
System.out.println("MyAdvice - after method:" + method.getName() + " call");
}
}


我们用工厂模式,生成具体的Target:

package proxy;

import java.lang.reflect.Proxy;

public class TargetFactory {

public static final int TRAGET_PROXY = 1;
public static final int TRAGET_CLASS = 0;

public static TargetInterface getTarget(int type) {
Target target = new Target();

if (type == TRAGET_PROXY) {

AdviceInterface advice = new MyAdvice();

MyInvocationHandler myHandler = new MyInvocationHandler(target, advice);

Class<?>[] interfaces = { TargetInterface.class };

TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(TargetInterface.class.getClassLoader(),
interfaces, myHandler);
return proxy;
} else {
return target;
}
}
}


Client 在调用的时候, 只依赖 TargetFactory 和 TargetInterface, 而不依赖具体的类:

package proxy;

public class ProxyDemo {

public static void main(String[] args) throws Exception {

TargetInterface target1 = TargetFactory.getTarget(TargetFactory.TRAGET_PROXY);
target1.say();
System.out.println(target1.getMsg());
System.out.println("--------");

TargetInterface target2 = TargetFactory.getTarget(TargetFactory.TRAGET_CLASS);
target2.say();
System.out.println(target2.getMsg());
}
}


上面代码中,target1 是一个Target代理, target2 是一个具体的Target,对于Client来说,是透明的。 

在实际开发中,我们可以把传入工厂方法 TargetFactory.getTarget(int )  的参数配置文件中, 我样就能动态改变Client使用的具体的类了。

运行结果:

MyAdvice - before method:say call
Hello World!
MyAdvice - after method:say call
MyAdvice - before method:getMsg call
MyAdvice - after method:getMsg call
messge from getMsg!
--------
Hello World!
messge from getMsg!


target1调用,除了执行自己的方法,还执行了 advice 的里方法, target2只执行了自己的方法。

eclipse 里目录结构:



 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息