Java dynamic proxy has been explained on the API connected to Java, so I won't write it here. What I understand:
Extend the functionality of a specific method in a specific interface, and this is the proxy. A proxy is a call method through a call handler object associated with a proxy instance.
Let’s take a look at it with an example:
interface:
public interface Num { void show(); int getNum(); int getProduct(int x);} Implementation class:
public class MyNum implements Num { @Override public int getNum() { return 3; } @Override public int getProduct(int x) { return x; } @Override public void show() { System.out.println("The underlying method prints the number 99"); }} Let's first look at how the invoke method is described in the API
That is to say, the call handler calls the underlying method representing the Method object to the implementation class object of the interface.
The first way to implement the proxy:
public class NumProxy { private Object num; //Construct the implementation class object of the interface through the constructor method public NumProxy(Object num) { this.num = num; } public Object getNumByProxy(){ Object numProxy = Proxy.newProxyInstance(num.getClass().getClassLoader(), new Class[]{Num.class}, new InvocationHandler() { /** * method: The Method instance corresponding to the interface method called on the proxy instance. What I understand is the real method instance of the proxy* args: What I understand is the parameter array of real methods*/ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object obj = null; System.out.println("Start logging before the method"); String methodName = method.getName(); if("getProduct".equals(methodName)){ obj = method.invoke(num, args); obj = (Integer) obj * 2; System.out.println("proxy: getProduct() ended"); } else if("show".equals(methodName)){ obj = method.invoke(num, args); System.out.println("proxy: show() ended"); } return obj; } }); return numProxy; }} The second way to implement proxy: by implementing the InvocationHandler interface
public class NumProxyImpl implements InvocationHandler { //Here I have concreted the interface type, and it is not written as Object private Num num; public NumProxyImpl(Num num){ this.num = num; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object obj = null; String methodName = method.getName(); if("getProduct".equals(methodName)){ System.out.println("proxy: getProduct() start"); obj = method.invoke(num, args); obj = (Integer) obj * 2; System.out.println("proxy: getProduct() end"); }else if("show".equals(methodName)){ System.out.println("proxy: show() start"); obj = method.invoke(num, args); System.out.println("proxy: show() end"); } return obj; }} Test code:
public class TestNum { public static void main(String[] args) { //Test NumProxy np = new NumProxy(new MyNum()); Num numProxy = (Num) np.getNumByProxy(); int x = numProxy.getProduct(2); System.out.println(x); numProxy.show(); System.out.println("---------------------"); NumProxyImpl npi = new NumProxyImpl(new MyNum()); Num numPro = (Num) Proxy.newProxyInstance(Num.class.getClassLoader(), new Class[]{Num.class}, npi); int n = numPro.getProduct(3); System.out.println(n); numPro.show(); }} Console results:
The second method is a little confused. I don't know if you have it. That is, it is called the invoke method in NumProxyImpl that is not displayed, but it was executed. Yes, let's go down to see this.
If you don’t want to be troublesome, just remember it.
For example, the encoding process can be used to use the proxy, and write an example next time.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.