JDK动态代理

JDK动态代理

1、动态代理实现方式

  1. 基于接口实现动态代理:JDK动态代理
  2. 基于继承实现动态代理:Cglib、Javassist动态代理

2、JDK动态代理

2.1 主要的类介绍

java.lang.reflect.Proxy:是所有代理类的父类,用于生成代理类或者代理实例。

getProxyClass:获取代理类的Class对象。

newProxyInstance:获取代理类实例。

java.lang.reflect.InvocationHandler:完成动态代理的整个过程。

invoke:整个代理过程实现。

2.2 代码实现

第一种实现方式:newProxyInstance

接口Caculator

package com.mine.proxy;

public interface Caculator {
	public int add(int i, int j);
	public int sub(int i, int j);
	public int mul(int i, int j);
	public int div(int i, int j);
}

实现类CaculatorImpl

package com.mine.proxy;

public class CaculatorImpl implements Caculator {

	public int add(int i, int j) {
		System.out.println("add excute");
		return i + j;
	}

	public int sub(int i, int j) {
		System.out.println("sub excute");
		return i - j;
	}

	public int mul(int i, int j) {
		System.out.println("mul excute");
		return i * j;
	}

	public int div(int i, int j) {
		System.out.println("div excute");
		return i / j;
	}
}

代理CaculatorProxy

package com.mine.proxy;

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

public class CaculatorProxy implements InvocationHandler {
	// 目标对象
	private Object target;
	
	public CaculatorProxy(Object target) {
		this.target = target;
	}
	
	// 获取代理对象
	public Object getProxy() {
		// 代理对象
		Object proxy = Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
		
		return proxy;
	}

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		String methodName = method.getName();
		System.out.println("invoke " + methodName + " before");
		Object result = method.invoke(target, args);
		System.out.println("invoke " + methodName + " after");
		return result;
	}
}

测试类Main

package com.mine.proxy;

public class Main {
	public static void main(String[] args) {
		// 目标对象
		Caculator caculatorImpl = new CaculatorImpl();
		
		// 获取代理对象
		Caculator proxy = (Caculator)new CaculatorProxy(caculatorImpl).getProxy();
		
		int result1 = proxy.add(1, 3);
		System.out.println("main result1: " + result1);
		
		int result2 = proxy.sub(5, 1);
		System.out.println("main result2: " + result2);
	}

}

第二种实现方式:newProxyInstance

代理类CaculatorProxy2

package com.mine.proxy;

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

public class CaculatorProxy2 implements InvocationHandler {
	// 目标对象
	private Object target;
	
	public CaculatorProxy2(Object target) {
		this.target = target;
	}
	
	// 获取代理对象
	public Object getProxy() throws Exception {
		// 代理对象
		Class<?> proxyClass = Proxy.getProxyClass(target.getClass().getClassLoader(), target.getClass().getInterfaces());
		Constructor<?> constructor = proxyClass.getConstructor(InvocationHandler.class);
		Object proxy = constructor.newInstance(this);
		return proxy;
	}

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		String methodName = method.getName();
		System.out.println("CaculatorProxy2.invoke " + methodName + " before");
		Object result = method.invoke(target, args);
		System.out.println("CaculatorProxy2.invoke " + methodName + " after");
		return result;
	}
	
	public static void main(String[] args) throws Exception {
		// 目标对象
		Caculator caculatorImpl = new CaculatorImpl();
		
		// 获取代理对象
		Caculator proxy = (Caculator)new CaculatorProxy2(caculatorImpl).getProxy();
		
		int result1 = proxy.add(1, 3);
		System.out.println("main result1: " + result1);
		
		int result2 = proxy.sub(5, 1);
		System.out.println("main result2: " + result2);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值