Java代理的两种实现方式

Java代理模式的两种实现方式

jdk动态代理

  1. 定义接口,被代理对象要实现接口
  2. 实现InvocationHandler
  3. 调用Proxy.newProxyInstance生成代理对象
package proxy;

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

public class InvocationHandlerImpl implements InvocationHandler {

    // 被代理对象
    private Object subject;

    public InvocationHandlerImpl(Object subject) {
        this.subject = subject;
    }

    /**
     * 负责集中处理动态代理类上所有的方法调用
     *
     * @param proxy  代理类的实例
     * @param method 被调用的方法对象
     * @param args   调用参数
     * @return
     * @throws Throwable
     */
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("前置通知");
        Object retVal = method.invoke(subject, args);
        System.out.println("后置通知");
        return retVal;
    }

    public static void main(String[] args) {
        Subject target = new RealSubject();
        ClassLoader classLoader = target.getClass().getClassLoader();
        Class<?>[] interfaces = target.getClass().getInterfaces();
        InvocationHandlerImpl invocationHandler = new InvocationHandlerImpl(target);
        Subject proxyInstance = (Subject) Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
        proxyInstance.sayHello("阿百川");
    }
}

interface Subject {
    public void sayHello(String name);

    public void sayGoodbye(String name);
}

class RealSubject implements Subject {
    public void sayHello(String name) {
        System.out.println("hello " + name);
    }

    public void sayGoodbye(String name) {
        System.out.println(name + " good bye !");
    }
}

cglib代理

  1. 自定义MethodInterceptor
  2. 通过Enhancer创建代理
package proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

class HelloService {

    public HelloService() {
        System.out.println("HelloService构造器");
    }

    final public String sayOthers(String name) {
        System.out.println("HelloService:sayOthers -> " + name);
        return null;
    }

    public void sayHello() {
        System.out.println("HelloService:sayHello");
    }

}
public class MyMethodInterceptor implements MethodInterceptor {

    /**
     *
     * @param proxy     cglib生成的代理对象
     * @param method    被代理对象的方法
     * @param args   方法入参
     * @param methodProxy   代理方法
     * @return
     * @throws Throwable
     */
    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        System.out.println("前置通知");
        Object retVal = methodProxy.invokeSuper(proxy, args);
        System.out.println("后置通知");
        return retVal;
    }

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(HelloService.class);
        enhancer.setCallback(new MyMethodInterceptor());
        HelloService helloService = (HelloService) enhancer.create();
        helloService.sayHello();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值