Javaのダイナミックプロキシの仕組み

Javaの動的プロキシは、以下の方法で実装できます。

  1. Proxy
  2. プロキシー
  3. 新しいプロキシインスタンス()
  4. InvocationHandler
  5. インヴォケーションハンドラー
  6. インボケーションハンドラ
  7. invoke() を呼び出す
public interface Subject {
    void doSomething();
}

public class RealSubject implements Subject {
    public void doSomething() {
        System.out.println("RealSubject doSomething");
    }
}

public class MyInvocationHandler implements InvocationHandler {
    private Subject target;

    public MyInvocationHandler(Subject target) {
        this.target = target;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("Before method invoke");
        Object result = method.invoke(target, args);
        System.out.println("After method invoke");
        return result;
    }
}

public class Main {
    public static void main(String[] args) {
        Subject realSubject = new RealSubject();
        Subject proxySubject = (Subject) Proxy.newProxyInstance(realSubject.getClass().getClassLoader(),
                realSubject.getClass().getInterfaces(),
                new MyInvocationHandler(realSubject));
        proxySubject.doSomething();
    }
}
  1. CGLib
  2. CGLib
  3. ネイティブな日本語で文章を言い換えてください。1つのオプションのみが必要です。ASM
public interface Subject {
    void doSomething();
}

public class RealSubject implements Subject {
    public void doSomething() {
        System.out.println("RealSubject doSomething");
    }
}

public class MyMethodInterceptor implements MethodInterceptor {
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        System.out.println("Before method invoke");
        Object result = proxy.invokeSuper(obj, args);
        System.out.println("After method invoke");
        return result;
    }
}

public class Main {
    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(RealSubject.class);
        enhancer.setCallback(new MyMethodInterceptor());
        Subject proxySubject = (Subject) enhancer.create();
        proxySubject.doSomething();
    }
}

なお、CGLib で動的プロキシを実装する際には、対象のクラスが final クラスまたは final メソッドを含むことはできません。これは、CGLibは対象のクラスのサブクラスを生成してプロキシを実装するためです。

  1. バイトバディ
  2. バイトバディ
public interface Subject {
    void doSomething();
}

public class RealSubject implements Subject {
    public void doSomething() {
        System.out.println("RealSubject doSomething");
    }
}

public class MyInterceptor implements MethodInterceptor {
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        System.out.println("Before method invoke");
        Object result = proxy.invokeSuper(obj, args);
        System.out.println("After method invoke");
        return result;
    }
}

public class Main {
    public static void main(String[] args) throws Exception {
        Class<? extends Subject> proxyClass = new ByteBuddy()
                .subclass(Subject.class)
                .method(any()).intercept(MethodDelegation.to(MyInterceptor.class))
                .make()
                .load(RealSubject.class.getClassLoader())
                .getLoaded();
        Subject proxySubject = proxyClass.getDeclaredConstructor().newInstance();
        proxySubject.doSomething();
    }
}

ByteBuddyを利用して動的プロキシを実現する際には、net.bytebuddy:byte-buddyとnet.bytebuddy:byte-buddy-agentの2つの依存関係を導入する必要があることに注意してください。

bannerAds