CGLIB FULL framework analysis: artifact to realize dynamic proxy
CGLIB (Code Generation Library) is a powerful code generating library that can dynamically generate proxy categories at runtime to achieve dynamic proxy.Compared with the standard dynamic proxy of Java (interface -based proxy), CGLIB can represent ordinary classes without interfaces.
## What is a dynamic agent?
In software design, the proxy mode is widely used in the interaction between objects.Dynamic proxy is a special proxy mode that creates proxy objects during runtime, rather than created during compilation.
The core idea of dynamic proxy is to replace the real object through a proxy class. The proxy class can intercept the method of the target object and perform additional operations before and after calling.In this way, we can add new functions or modify existing functions without changing the original code.
## CGLIB's working principle
CGLIB uses the byte code library ASM (a lightweight Java bytecode operation and analysis framework) to generate the byte code of the agent class.It creates a proxy class through inheritance and rewritten the real object to insert additional logic before and after the method.In this way, we can use the method of calling the proxy class to intercept the call of the real object method.
Unlike Java's standard dynamic proxy, CGLIB does not require real objects to implement any interface.It can proxy ordinary classes, specific classes, or the class of interfaces.
## cglib use examples
Next, we will demonstrate the use of CGLIB through a simple example.
First, we need to add CGLIB to our project.In the Maven project, we can add the following code to the `pom.xml` file:
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.4.0</version>
</dependency>
Then, we define a target class `UserService`, which contains a` Sayhello` method:
public class UserService {
public void sayHello() {
System.out.println("Hello, World!");
}
}
Next, we create a proxy class `ProxyInterceptor`, which inherits the` MethodinterCePTOR` interface and implement the `intercept` method.In the `intercept` method, we can perform additional operations before calling the target method by the` MethodProxy` object:
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class ProxyInterceptor implements MethodInterceptor {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("Before method execution");
Object result = proxy.invokeSuper(obj, args);
System.out.println("After method execution");
return result;
}
}
Finally, we can use CGLIB to generate proxy objects and call the target method:
import net.sf.cglib.proxy.Enhancer;
public class Main {
public static void main(String[] args) {
// Create an enhancer object
Enhancer enhancer = new Enhancer();
// Set the target class
enhancer.setSuperclass(UserService.class);
// Set the callback object
enhancer.setCallback(new ProxyInterceptor());
// Generate proxy objects
UserService proxy = (UserService) enhancer.create();
// Call the target method
proxy.sayHello();
}
}
Run the above code, the output will be:
Before method execution
Hello, World!
After method execution
Through the use of CGLIB, we successfully created an agent object and exported relevant information before and after calling the target method.
Summarize:
CGLIB is a powerful dynamic proxy library. By using bytecode technology to generate proxy, it can realize dynamic agents for ordinary class without having to implement interfaces.Through CGLIB, we can easily add additional functions or modify existing functions to target objects.