The Java.lang.reflect package provides a proxy class and a Invocationhandler interface for generating dynamic proxy classes and dynamic proxy objects.
First, use proxy, invocationhandler create dynamic Agent
Note here that there is also a proxy class under the java.net package, but this class is used to set up the proxy server, not to confuse it.
Proxy provides the following two ways to create dynamic proxy classes and dynamic proxy instances:
In fact, even if the first method is used to create the dynamic proxy class, if the program needs to create the object through the proxy class, it still needs to pass in a Invocationhandler object, that is, each system-generated proxy object has a Invocationhandler object associated with it.
Public classProxytest { Public Static voidMain (string[] args) {Myinvocationhandler Invocationhandler=NewMyinvocationhandler (); Invocationhandler.settarget (NewHero ()); Person Hero= (person) proxy.newproxyinstance (Hero.class. getClassLoader (), Hero.class. Getinterfaces (), invocationhandler);//hero.fight ();//Hero.laugh ();Hero.sayhello ("Tom"); Hero.work (); }}Interfaceperson{voidWork (); voidSayHello (String name);}Interfacefightable{voidfight ();}classHeroImplementsperson,fightable{@Override Public voidfight () {System.out.println ("Hero Fight"); } @Override Public voidWork () {System.out.println ("Hero Work"); } @Override Public voidSayHello (String name) {System.out.println ("Hello" +name); } Public voidlaugh () {System.out.println ("Hero Laugh"); }}classMyinvocationhandlerImplementsinvocationhandler{PrivateObject Target; Public voidsettarget (Object target) { This. target =Target; } @Override PublicObject Invoke (Object proxy, Method method, object[] args)throwsthrowable {System.out.println ("-------the method being executed:" +method); if(Args! =NULL) {System.out.println (The following is an argument that executes the method passed in:); for(Object Val:args) {System.out.println (val); } } Else{System.out.println ("The method called does not have an argument"); Method.invoke (target); } return NULL; }}
Note that the object generated by the Java Dynamic Agent must be pointed to by the interface reference, because the dynamic agent of Java actually uses the incoming interface type to create a new class, so the hero type cannot be used to point to the created object in the code above. Can only be pointed to by the person or fightable type. Look at the source code can find this point.
Second, the AOP agent
One way to implement AOP (aspect-oriented programming) is to use a Java dynamic proxy.
AOP is actually implemented in the code above, and the System.out.println method for all proxy objects is executed before the method body is executed ("the method called does not have an argument"); It's just that the code is written in Invocationhandler, and frameworks like spring decouple this part of the code from the design pattern, but that's essentially the way it works.
Java Core Learning (40) using reflection to generate a JDK dynamic agent