JDK dynamic proxy learning
1. What is JDK dynamic proxy
JDK dynamic proxy is to create a proxy class to implement the enhancement of the interface method of the proxy class during the running of the program (only objects that implement the interface method can be proxied), which is different from the static proxy that creates a proxy object in advance. The bottom layer uses Java reflection technology.
2. Why use JDK dynamic proxy
To increase the readability, scalability, and maintainability of the system (less code, lazy), the famous spring framework uses JDK dynamic proxy, the most typical AOP feature of spring, which can realize transaction management and message notification , logging, security monitoring, etc., so that developers only need to pay attention to business details, without considering these basic general functions.
3. How to learn JDK dynamic proxy
1. Learn Java, learn reflection
2. Learn design patterns
(Big Talk Design Patterns)
(the beauty of design patterns—Wang Zhengqian Google engineer)
3. Think first, and then write it by yourself
The key object
InvocationHandler interface to realize JDK dynamic proxy
is obvious. It is in the reflection package of Java, so he uses reflection. This interface has only one method invoke, and the logic that needs to be enhanced is written here
package java.lang.reflect;
public interface InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable;
}
Proxy object
The Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h) method generates a proxy object through reflection, and implements a series of security access policies in this method.
loader: the object's class loader
interfaces: the object's interface
InvocationHandler implements enhanced processing of the interface
Hands-on implementation
1. Create a new interface
public interface IUser {
void cry();
void slime();
void sing();
}
2. Create a new interface implementation class
public class User implements IUser {
@Override
public void cry() {
System.out.println("cry");
}
@Override
public void slime() {
System.out.println("slime");
}
@Override
public void sing() {
System.out.println("sing");
}
}
3. Create a new processor to implement the InvocationHandler interface
public class DynmenicHandle implements InvocationHandler {
private Object proxyObject;
public DynmenicHandle(Object proxyObject){
this.proxyObject=proxyObject;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Exception{
Object result = method.invoke(proxyObject, args);
System.out.println("此次JDK动态代理方法:"+method.getName());
return null;
}
}
4. Create a new proxy class
public class DynmenicProxy {
public Object createProxy(Object proxiedObject){
Class[] interfaces = proxiedObject.getClass().getInterfaces();
DynmenicHandle handler = new DynmenicHandle(proxiedObject);
return Proxy.newProxyInstance(proxiedObject.getClass().getClassLoader(), interfaces, handler); }
}
5. main method
public class Main {
public static void main(String[] args) {
DynmenicProxy dynmenicProxy=new DynmenicProxy();
IUser user=(IUser)dynmenicProxy.createProxy(new BUser());
user.cry();
user.sing();
user.slime();
}
}
6. Results
cry
此次JDK动态代理方法:cry
sing
此次JDK动态代理方法:sing
slime
此次JDK动态代理方法:slime
4. Summary
When I first started learning JDK dynamic proxies, I knew only a little about it. As the amount of code increased, one project after another was developed, and when I came back to learn JDK dynamic proxies, I suddenly realized that the beauty of Wang Zheng’s design patterns inspired me a lot. I recommend everyone to check it out.