Java development commonly used dynamic proxy solutions: JDK agent and CGLIB agent comparative analysis
Java development commonly used dynamic proxy solutions: JDK agent and CGLIB agent comparative analysis
Dynamic proxy is a technology that generates an agent object during runtime. It can enhance the function of the original object through the proxy object without modifying the original code.In Java development, the commonly used dynamic proxy solutions include JDK agents and CGLIB proxy.The two are different in implementation, and the two dynamic agency solutions will be analyzed below.
1. JDK proxy
The JDK proxy is a dynamic proxy solution that comes with Java. It is implemented using the `proxy` and` Invocationhandler` in Java.lang.reflet` in Java to achieve it.The JDK agent can only be the interface, and the proxy object needs to be generated through the interface.Its principle is to dynamically generate a proxy class that realizes the target interface and call the target object through the proxy class.
Here are a sample code using JDK proxy:
// Define the target interface
public interface UserService {
void addUser(String username, String password);
}
// Implement the original class of the target interface
public class UserServiceImpl implements UserService {
public void addUser(String username, String password) {
System.out.println ("Add user:" + username + ", password:" + password);
}
}
// Customly define InvocationHandler implementation classes
public class MyInvocationHandler implements InvocationHandler {
private Object target;
public MyInvocationHandler(Object target) {
this.target = target;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println ("Before the target method");
Object result = method.invoke(target, args);
System.out.println ("After the target method");
return result;
}
}
// Create an agent object and call the target method
public class Main {
public static void main(String[] args) {
UserService userService = new UserServiceImpl();
MyInvocationHandler invocationHandler = new MyInvocationHandler(userService);
UserService proxy = (UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(),
userService.getClass().getInterfaces(), invocationHandler);
proxy.addUser("admin", "123456");
}
}
2. CGLIB proxy
The CGLIB agent is a dynamic proxy solution based on the third -party library CGLIB.CGLIB is a powerful high -performance bytecode generating library. It can dynamically generate a subclass of the target class at runtime and rewrite the method of the target class to achieve the proxy function.The CGLIB proxy does not require the target object to implement the interface, so it can represent the ordinary class.
The following is an example code using CGLIB proxy:
// Objective class
public class UserService {
public void addUser(String username, String password) {
System.out.println ("Add user:" + username + ", password:" + password);
}
}
// Customize MethodInterceptor implementation classes
public class MyInterceptor implements MethodInterceptor {
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println ("Before the target method");
Object result = proxy.invokeSuper(obj, args);
System.out.println ("After the target method");
return result;
}
}
public class Main {
public static void main(String[] args) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(UserService.class);
enhancer.setCallback(new MyInterceptor());
UserService proxy = (UserService) enhancer.create();
proxy.addUser("admin", "123456");
}
}
The comparative analysis of JDK agents and CGLIB proxy is as follows:
1. The JDK agent can only be the interface, while the CGLIB agent can represent the ordinary class.
2. The JDK proxy dynamically generates a proxy class that achieves the target interface, while the CGLIB agent dynamically generates a subclass of the target class during runtime.
3. The JDK proxy to process the proxy logic through the implementation class of the `Invocationhandler`, and the CGLIB agent implements the proxy logic through the class of the CGLIB proxy to implement the class.