Dynamic Proxy in Java
The implementation of dynamic proxy in JDK that only supports interfaces is as follows:
- Call static methods in java.lang.reflect.Proxy
newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler h)
Loader and interfaces are interfaces of the proxied class that transform the returned object into the proxied class
2. Implement a java.lang.reflect-INvocationHandler interface and implement its methods
public Object invoke(Object proxy, Method method, Object[] args)
The key here is to pass in a concrete class as an instance variable of this implementation class, and ensure that it is called in the following way, and add the logic that needs to be delegated before and after the method call
Method. voice (represented class instance, args);