(#简介)
What is a dynamic agent? Dynamic agent is the implementation phase does not care who the agent is, and in the run phase to specify which proxy object is, dynamic agent in the framework of the use of very
Wide, such as spring's AOP, the core is the use of dynamic proxy mechanism, let's look at how to implement a dynamic proxy mode
(#实现)
First, let's define an interface.
Public interface ICar { void run (String name);}
Then we'll define an implementation class
public class Car implements ICar {public void run (String name) { System.out.println (name+ "car is Running"); }}
Next, let's implement a generic dynamic Proxy tool class
public class objectproxy{Public <T> T Create (Final T t) { return (t) proxy.newproxyinstance ( ObjectProxy.class.getClassLoader (), T.getclass (). Getinterfaces (), new Invocationhandler () {public Object Invoke (Object Proxy, Method method, object[] args) throws Throwable {
Before Invoke return Method.invoke (t, args);
After invoke } );} }
In this tool class we use generics to make the method more generic, and if you have a lack of understanding of generics, see this blog: 1190000002646193
Finally, let's take a look at the call:
public static void Main (String[]args) { ICar car = new car (); ObjectProxy carproxy = new ObjectProxy (); ICar Proxycar = carproxy.create (car); Proxycar.run ("Benchi"); }
If there is no accident, running the main method will appear: Benchi car is running. Of course, the most powerful point of using dynamic proxies is that you can do some before and after things when you call this method.
This operability is very large, for some features particularly applicable
(#) Expand
In the reflection of a relatively powerful library cglib, in some specific scenarios using fast reflection is necessary, the common JDK agent is a bit more rubbing, is only the agent implementation of the interface, but not the method of proxy itself
This time to realize the function of dynamic agent will need to use to Cglib, OK, here is a chestnut:
First extend the original implementation class: Add a method of its own nrun, let us implement its proxy
public class Car implements ICar {public void run (String name) { System.out.println (name+ "car is Running"); } public void Nrun (String name) { System.out.println (name+ "I am my own Run");} }
The first implementation of:
public class Fastproxy {public <T> t create (Final T t) { enhancer enhancer = new enhancer (); Enhancer.setsuperclass (T.getclass ()); Enhancer.setcallback (New Methodinterceptor () {Public object intercept (Object o, Method, object[] objects, Methodproxy methodproxy) throws Throwable { return Methodproxy.invoke (t,objects); } ); Return (T) enhancer.create ();} }
The second implementation:
public class Fastproxy {public <T> t create (Final T t) { enhancer enhancer = new enhancer (); Enhancer.setsuperclass (T.getclass ()); Enhancer.setcallback (New Methodinterceptor () {Public object intercept (Object o, Method, object[] objects, Methodproxy methodproxy) throws Throwable { Fastclass fastclass = Fastclass.create (T.getclass ()); Fastmethod Fastmethod = Fastclass.getmethod (Method.getname (), method.getparametertypes ()); Return Fastmethod.invoke (t,objects); } }); Return (T) enhancer.create ();} }
There is no difference in the invocation of the main method, so that we implement a dynamic proxy for our own methods.
Lao Wang speaks homemade RPC framework. (two. Dynamic agent)