跳至主要內容

代理模式

LiCheng小于 1 分钟

代理模式

  • 增加对象的行为
  • jdk代理,cglib代理

jdk代理

  • 只代理接口,代理实现继承了 Proxy 类,所以就不能使用子类去创建代理了
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author lc
 * @since 2022/6/8
 */
public class ProxyTest {
    public static void main(String[] args) throws Exception {
        Mapper m = id -> { //实现类
            System.out.println("HelloWorld");
            return null;
        };
        Mapper mapper = (Mapper) Proxy.newProxyInstance(Mapper.class.getClassLoader(), new Class[]{Mapper.class}
                , new ProxyMapper(m));
        System.out.println(mapper.selectOne("1"));
    }
}
interface Mapper{
    String selectOne(String id);
}
class ProxyMapper implements InvocationHandler{
    public Object pro;

    public ProxyMapper(Object pro) {
        this.pro = pro;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("拦截执行!");
        System.out.println("获取方法名:"+method.getName());
        return method.invoke(pro, args);
    }
}

cglib代理