package com.ice.structure.Proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; /** * @author ice * @blog https://blog.csdn.net/dreaming_coder * @description * @create 2021-10-31 14:13:44 */ // 这个类自动生成代理类 public class ProxyInvocationHandler implements InvocationHandler { // 被代理的接口 private Rent rent; public void setRent(Rent rent) { this.rent = rent; } // 动态生成代理对象 public Object getProxy() { return Proxy.newProxyInstance(this.getClass().getClassLoader(), rent.getClass().getInterfaces(), this); } @Override // 处理代理实例,并返回结果 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("代理中介带你看房"); Object result = method.invoke(rent, args); System.out.println("收中介费"); return result; } }