Java Dynamic proxy and java Dynamic proxy
The dynamic proxy of java is described on the java api, so I will not write it here. Proxy I understand:
Extends the functions of specific methods in a specific interface. This is the proxy. A proxy is a method associated with a proxy instance to call a handler object.
The following is 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 ("bottom-layer method Printing Number 99 ");}}
First, let's take a look at how the invoke Method described in the api.
That is to say, the calling handler calls the underlying Method represented by the Method object to the interface implementation class object.
The first way to implement Proxy:
Public class NumProxy {private Object num;
// Construct the implementation Class Object public NumProxy (Object num) {this. num = num;} public Object getNumByProxy () {Object numProxy = Proxy. newProxyInstance (num. getClass (). getClassLoader (), new Class [] {Num. class}, new InvocationHandler () {/*** method: corresponds to the interface method called on the proxy instanceMethodInstance. I understand the real Method Instance x args: I understand the parameter array of the real method */@ Override public Object invoke (Object proxy, Method method, object [] args) throws Throwable {Object obj = null; System. out. println ("Start recording before method"); String methodName = method. getName (); if ("getProduct ". equals (methodName) {obj = method. invoke (num, args); obj = (Integer) obj * 2; System. out. println ("proxy: getProduct () End");} else if ("show ". equals (methodName) {obj = method. invoke (num, args); System. out. println ("proxy: show () ended") ;}return obj ;}}); return numProxy ;}}
Method 2: implement the proxy through the InvocationHandler Interface
Public class NumProxyImpl implements InvocationHandler {
// Here I have embodied the interface type, but 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 () ended");} 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 result:
The second method is a bit confusing. I don't know if you have any. That is, the invoke method in NumProxyImpl is not displayed, but it is executed. Well, let's take a look at it.
You just need to remember it.
For example, you can use a proxy for encoding, and write an example next time.