Skip to content

Instantly share code, notes, and snippets.

@yuuow
Last active May 29, 2020 02:37
Show Gist options
  • Save yuuow/ebd4803ff5f8def546e2e2ffb3ea56b1 to your computer and use it in GitHub Desktop.
Save yuuow/ebd4803ff5f8def546e2e2ffb3ea56b1 to your computer and use it in GitHub Desktop.

JDK Dynamic proxy can only proxy by interface (so your target class needs to implement an interface, which is then also implemented by the proxy class).

CGLIB (and javassist) can create a proxy by subclassing. In this scenario the proxy becomes a subclass of the target class. No need for interfaces.

So Java Dynamic proxies can proxy: public class HelloImpl implements Hello where CGLIB can proxy: public class HelloImpl

ref: https://stackoverflow.com/questions/10664182/what-is-the-difference-between-jdk-dynamic-proxy-and-cglib

package me.xunco.proxy;

public interface Hello {
    String sayHello();
}
package me.xunco.proxy;

public class HelloImpl implements Hello {
    @Override
    public String sayHello() {
        return "hhhhhh";
    }
}
package me.xunco.proxy;

public class Test {
    public static void main(String[] args) {
        Test test = new Test();

        test.getStaticProxy();
        System.out.println("--------------");

        test.getJdkProxy();
        System.out.println("--------------");

        test.getCglibProxy();
        System.out.println("--------------");

    }

    public void getStaticProxy() {
        StaticProxy proxy = new StaticProxy();
        proxy.setHello(new HelloImpl());
        System.out.println(proxy.sayHello());
    }


    public void getJdkProxy() {
        JdkDynamicProxy proxy = new JdkDynamicProxy(new HelloImpl());
        Hello hello = (Hello) proxy.getProxyInstance();

        System.out.println(hello.sayHello());
    }

    public void getCglibProxy() {
        CglibDynamicProxy proxy = new CglibDynamicProxy(new HelloImpl());
        HelloImpl hello = (HelloImpl) proxy.getProxyInstance();

        System.out.println(hello.sayHello());
    }
}
package me.xunco.proxy;

public class StaticProxy implements Hello {

    private Hello hello;

    public void setHello(Hello hello) {
        this.hello = hello;
    }

    @Override
    public String sayHello() {
        System.out.println("Static proxy...");
        return hello.sayHello();
    }
}
package me.xunco.proxy;

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

public class JdkDynamicProxy implements InvocationHandler {

    private final Object target;


    public JdkDynamicProxy(Object target) {
        this.target = target;
    }

    public Object getProxyInstance() {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("Jdk dynamic proxy...");
        return method.invoke(target, args);
    }
}
package me.xunco.proxy;

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

import java.lang.reflect.Method;

public class CglibDynamicProxy implements MethodInterceptor {

    private final Object target;

    public CglibDynamicProxy(Object target) {
        this.target = target;
    }

    public Object getProxyInstance() {
        Enhancer enhancer = new Enhancer();

        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);

        return enhancer.create();
    }


    @Override
    public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        System.out.println("Cglib dynamic proxy...");
//        return method.invoke(target, args);
        return methodProxy.invokeSuper(object, args);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment