Java中动态生成代理的方法

本文探讨了Java中的代理模式,介绍了如何动态生成代理,避免编写额外的代理类。通过示例展示了如何使用`java.lang.reflect.Proxy`创建代理,实现在目标方法调用前后添加额外操作。此外,还提到了工厂模式的应用,使得客户端代码能根据配置动态选择使用的目标类。
摘要由CSDN通过智能技术生成

本文介绍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 里目录结构:


 










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值