CGLIB of dynamic proxy

Introduction to CGLIB

CGLib (Code Generation Library) is a powerful, high-performance, high-quality code generation library.

CGLib can extend Java classes and implement Java interfaces at runtime. CGLib is stronger than Java's java.lang.reflect.Proxy class in that it can not only take over the methods of the interface implementation class, but also the methods of ordinary classes.

The bottom layer of CGLib is the Java bytecode manipulation framework - ASM.

 

                     cglib organization chart

 

cglib implements proxy for classes. The principle is to generate a subclass for the specified target class and override the method to achieve enhancement. However, because inheritance is used, it cannot proxy the final modified class. 

 

proxy mode 

The proxy pattern is a common Java design pattern. Its characteristic is that the proxy class has the same interface as the delegate class. The proxy class is mainly responsible for preprocessing messages for the delegate class, filtering messages, forwarding messages to the delegate classes, and post-processing messages. There is usually an association relationship between the proxy class and the delegate class. An object of a proxy class is associated with an object of a delegate class. The object of the proxy class itself does not really implement the service, but by calling the relevant methods of the object of the delegate class, provide specific services. 

According to the creation period of the agent, the agent class can be divided into two types. 

Static proxy : Created by a programmer or automatically generated by a specific tool, and then compiled. Before the program runs, the .class file of the proxy class already exists. 

Dynamic proxy : It is dynamically created using reflection mechanism when the program is running. 

 

Code Demo:

Proxy class:

public class CglibProxy implements MethodInterceptor {

    /**
     * Create a proxy object for the object based on the class object
     * 1. Set the parent class; 2. Set the callback
     * Essence: Dynamically creates a subclass of a class object
     */
    public <T> T getProxy(Class<T> cls) {
        return (T) Enhancer.create(cls, this);
    }

    // callback method
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        before();
        Object result = proxy.invokeSuper(obj, args);
        after();
        return result;
    }

    private void after() {
        System.out.println("after proc");
    }

    private void before() {
        System.out.println("before proc");
    }

}

 

Enhancer's create method:

    public static Object create(Class type, Callback callback) {
        Enhancer e = new Enhancer();
        e.setSuperclass(type);
        e.setCallback(callback);
        return e.create();
    }

According to the create method, cglib dynamically generates proxy classes by taking the class to be proxied as the parent class and generating its subclasses. 

 

Normal Java class (doesn't implement any interface)

public class Lion{
    public void run() {
        System.out.println("The king of the grassland - the lion is running");
    }
}

 

test class

public class AppTest {

    @Test
    public void testCglib() {
        CglibProxy cglibProxy = new CglibProxy();
        Lion lionProxy = cglibProxy.getProxy(Lion.class);

        lionProxy.run();
    }

}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326310008&siteId=291194637