Limitations of Java SDK dynamic proxies

We know that if you want to use the dynamic proxy Java SDK, so we have to create a proxy interface, and returns the proxy object also can switch to an interface type, but if have a class, it has no interface, we also agent of his function, or hope of interface agent method, is not so good. This is where the third-party library Cglib solves the problem.

Cglib dynamic proxy

Let’s start with an example

public class SimpleCGLibDemo {
    // Represents the proxied class
    static class RealService {
        public void sayHello(a) {
            System.out.println("hello"); }}static class SimpleInterceptor implements MethodInterceptor {
        @Override
        public Object intercept(Object object, Method method, Object[] args, MethodProxy proxy) throws Throwable {
            System.out.println("entering " + method.getName());
            Object result = proxy.invokeSuper(object, args);
            System.out.println("leaving " + method.getName());
            returnresult; }}@SuppressWarnings("unchecked")
    // Generate a proxy object for the proxied class
    private static <T> T getProxy(Class<T> cls) {
        Enhancer enhancer = new Enhancer();
        // Sets the proxied class
        enhancer.setSuperclass(cls);
        // Set the propped class's public non-final method to handle when called
        enhancer.setCallback(new SimpleInterceptor());
        return (T) enhancer.create();
    }

    public static void main(String[] args) throws Exception { RealService proxyService = getProxy(RealService.class); proxyService.sayHello(); }}Copy the code

As you can see, to use it, simply implement the MethodInterceptor interface on the proxy class and override the Intercept method. In fact, we can add a MethodProxy proxy parameter to the Intercept method overridden by the InvocationHandler interface in the Biao Java SDK.

Java SDK dynamic proxy Cglib dynamic proxy
Inherited interface InvocationHandler MethodInterceptor
Overriding methods invoke intercept
Override method parameters Object proxy, Method method, Object[] args Object object, Method method,Object[] args, MethodProxy proxy
Execute method code method.invoke(realObj,args); proxy.invokeSuper(object, args);

The table above illustrates the specific differences between the two dynamic proxy applications. Since Cglib does not depend on a specific interface, it is implemented through inheritance

Comparison of the two

  • The Java SDK agent
  1. Java SDK agents are geared towards a set of interfaces
  2. It creates an implementation class for these interfaces on the fly, and you can customize the specific logic of the interface in InvocationHandler
  3. Implementations are custom and may not necessarily have a real propped object behind them, but may have multiple real objects (an interface can be implemented by multiple classes)
  4. It is actually an object, not a concrete class
  5. There is an actual object, and the custom InvocationHandler refers to that object, dynamically creates a proxy class, and the proxy object, accesses the proxy object, and the proxy object calls the methods of the actual object
  • Additional agent
  1. The proxy is a concrete class with only one object, based on inheritance implementation
  2. It also creates a class dynamically whose parent is the propped class and overrides all its public non-final methods