> No proxy
Sometimes I want to print some logs before and after some methods, so I have the following code.
This is a method of handling float type addition, I would like to print a parameter before calling it, after the call to print the results of the calculation. (As for why not directly with the + number operation, see "Java" float calculation is inaccurate)
Packagecom.nicchagil.study.java.demo.No09 Agent. No01 no proxy;ImportJava.math.BigDecimal; Public classFloatcalculator { Public floatAddfloatAfloatb) {BigDecimal B1=NewBigDecimal (A + "")); BigDecimal B2=NewBigDecimal (b + "")); floatf =B1.add (B2). Floatvalue (); returnF; }}
I want to print before and after it runs, the most straightforward way is when the call is printed
Packagecom.nicchagil.study.java.demo.No09 Agent. No01 no proxy; Public classCall { Public Static voidMain (string[] args) {floatF1 =1f; floatF2 =1f; System.out.println ("F1" + F1 + ", F2" +F2); floatresult =NewFloatcalculator (). Add (F1, F2); System.out.println ("Result" +result); }}
I am glad to see this log!
F1, 1.0, F2, 1.0, 2.0
> Static Proxy
As the project gets bigger, the place to call this method becomes more and more, and if there are 10 calls to the place, I'm not going to write 100 times to print the method.
At this point, the static proxy method can help us.
Define an interface
Package com.nicchagil.study.java.demo.No09 agent. No02 static agent; Public Interface ICalculator { /** * <p>add</p> ** public float Add (floatfloat b); }
Real Business Class
Packagecom.nicchagil.study.java.demo.No09 agent. No02 static agent;ImportJava.math.BigDecimal; Public classFloatcalculatorImplementsICalculator {@Override Public floatAddfloatAfloatb) {BigDecimal B1=NewBigDecimal (A + "")); BigDecimal B2=NewBigDecimal (b + "")); floatf =B1.add (B2). Floatvalue (); returnF; } }
The proxy class, in this class, handles the task of performing the actual business and also bundling the print log
Packagecom.nicchagil.study.java.demo.No09 agent. No02 static agent; Public classFloatcalculatorproxyImplementsICalculator {ICalculator C=NULL; /*** Construction Method *@paramC objects that need to be proxied*/ PublicFloatcalculatorproxy (ICalculator c) {Super(); This. C =C; } @Override Public floatAddfloatF1,floatF2) {System.out.println ("F1" + F1 + ", F2" +F2); floatresult = This. C.add (f1, F2); System.out.println ("Result" +result); returnresult; } }
Then, when we call, we just call the proxy class, not only to calculate the results, but also to come out of the log.
Package com.nicchagil.study.java.demo.No09 agent. No02 static agent; Public class Call { publicstaticvoid main (string[] args) { System.out.println ("Object of the Agent:") ; New Floatcalculatorproxy (new floatcalculator ()); C2.add (1f, 1f);} }
I was calm when I saw the log.
2.0, 1.0, F2,1.0
> Dynamic Agent
If now not only floatcalculator this class need to print the log, there are other kinds also need to print the log, then we do not have to write a number of proxy classes?
At this time, Java API outraged, the dynamic agent. Dynamic proxies are implemented for us through the reflection mechanism.
Interface Class (ICalculator), Real business Class (Floatcalculator) as static proxy, no longer duplicates
Call Processing class
Packagecom.nicchagil.study.java.demo.No09 agent. No03 dynamic agent;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method; Public classDynamicproxyhandlerImplementsInvocationhandler {PrivateObject proxied =NULL; PublicDynamicproxyhandler (Object proxied) { This. proxied =proxied; } @Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsthrowable {System.out.println ("Clazz" +Proxy.getclass ()); System.out.println ("Method--" +method); for(inti = 0; i < args.length; i++) {System.out.println ("args[" + i + "]" +Args[i]); } Object result=Method.invoke (proxied, args); System.out.println ("Result--" + (Result! =NULL? Result.tostring (): "")); returnresult; }}
Call class
Packagecom.nicchagil.study.java.demo.No09 agent. No03 dynamic agent;ImportJava.lang.reflect.Proxy;Importcom.nicchagil.study.java.demo.No09 agent. No02 static proxy. Floatcalculator;Importcom.nicchagil.study.java.demo.No09 agent. No02 static proxy. ICalculator; Public classCall { Public Static voidMain (string[] args) {/*Agent's Object*/System.out.println ("Agent's object:"); ICalculator C2= (ICalculator) proxy.newproxyinstance (ICalculator.class. getClassLoader (),NewClass[] {ICalculator.class},NewDynamicproxyhandler (NewFloatcalculator ())); C2.add (1f, 1f); }}
Log
Agent's object: Clazz class $Proxy 0method Public Abstract float com.nicchagil.study.java.demo.No09 proxy. No02 static proxy. Icalculator.add (float,float ) args[0], 1.0args[1], 1.0result , 2.0
Temporarily finished.
"Java" Generation processing? Proxy mode-static proxy, dynamic proxy