By example, We first establish an interface Interface1.java to implement dynamic proxy, the code is as Follows:
package my.spring.fuck.aoptest; public Interface Interface1 { publicvoid Say_hello ();}
A very simple interface, and then define an implementation of this interface class myimple.java, code:
package my.spring.fuck.aoptest; public class Implements interface1{ @Override publicvoid Say_hello () { System.out.println ( "hello world");} }
Here is the proxy class we want to create, the creation of the proxy class after the completion of the dynamic agent, is the most important step (ps: although this is said proxy class, but it is not a proxy class, Look at the code can see that this class implements the Java.lang.reflect.InvocationHandler interface, invocationhandler means: "call handler", meaning that we implement the dynamic agent, is handled by this class, And the real proxy class is the Java.lang.reflect.Proxy class, ok, a bit raozui, the agent has detailed comments, the code is as Follows:
packagemy.spring.fuck.aoptest;Importjava.lang.reflect.InvocationHandler;Importjava.lang.reflect.Method;Importjava.lang.reflect.Proxy;//This class implements the Invocationhandler interface, through the API view, this interface has only one method, that is, the following override of the Invoke method, below the detailed comment public classMyproxyImplementsInvocationhandler{Object object1;//used to store the class being proxied, This example Myimple.java//bind= binding, is our custom method, the formal parameter of this method is the object type, is used to receive the proxy class, This example is myimple.java, and then return a proxy class, the method has a detailed comment publicObject bind (object Someobject) { this. object1=someobject; returnProxy.newproxyinstance (object1.getclass (). getclassloader (), object1.getclass (). getinterfaces (), this); //proxy= proxy, This method returns an instance of a proxy class, through the operation of this instance, you can implement a dynamic proxy, look at the above three parameters, parameter 1 is the class loader, parameter 2 is the object of the agent, parameter 3//is the handler of the agent, through this method to obtain the Object's proxy (proxy), The agent stores the execution method when the handler (in this case, this class, that is, myproxy, is a class that implements the Invocationhandler Interface) } //the following method (invoke) is the method in interface invocationhandler, which invokes the Invoke method when the proxy calls this class;//invoke is also a method in java.lang.reflect.Method, each of the methods of each class belongs to the method class, here no longer repeat@Override publicObject Invoke (object proxy, method method, object[] Args)throwsThrowable {//the 3 Parameters of this method represent the proxy class, the method, the parameter list of the method, and the system automatically assigns the valueSYSTEM.OUT.PRINTLN ("AOP Dynamic proxy, processing before method execution"); Object Return_obj=method.invoke (object1, args);//the original method of executing the proxy classSYSTEM.OUT.PRINTLN ("AOP Dynamic agent, processing after method execution"); returnreturn_obj; } public Static voidmain (string[] Args) {myproxy myproxy=Newmyproxy (); Interface1 Proxy= (Interface1) Myproxy.bind (NewMyimple ()); Proxy.say_hello (); }}
Run the myproxy class, console output such as:
Detailed Spring AOP dynamic agent