In the previous blog briefly wrote a static proxy, here is the main talk about dynamic agents
There are two main types of dynamic agents
JDK Dynamic Agent cglib Dynamic Agent
What is the difference between these two agents?
(Summarize according to your own comprehension)
1.JDK Dynamic Agent
His characteristic is that the target object must have an interface
His essence is: An implementation class that creates an interface
The timing of his run: when the program runs
2.CGLIB Dynamic Agent
He is characterized by an agent that does not have an interface on a type
His essence is: to build a subclass of the target type in memory
The timing of his operation was: compile-time
After a brief introduction of these two agents, we use an example to see how to implement Dynamic proxy
Do the JDK dynamic agent first
To prepare an interface Isomeservice, the interface has a method Dosome (), and an implementation class Someserviceimpl for this interface, and overrides the method in which the code is as follows
Package demo15;/** * Created by mycom on 2018/3/8. */public interface isomeservice{public void Dosome ();}
Package demo15;/** * Created by mycom on 2018/3/8. */public class Someserviceimpl implements Isomeservice {public void Dosome () { System.out.println ("10:10 review"); }}
Using the JDK dynamic Proxy does not need to be configured in the configuration file, so it is now tested directly, but it is not possible to use a single test, but to test it using the main method.
Package Demo15;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;import java.lang.reflect.proxy;/** * Created by mycom on 2018/3/8. */public class Test {public static void main (string[] args) {//First create an implementation class for an interface final Someserviceimpl servi Ce=new Someserviceimpl (); Before calling the method, you want to log logs with dynamic agents, generate a dynamic proxy, and return the interface Isomeservice proxyinstance = (isomeservice) proxy.newproxyinstance (service.get Class (). getClassLoader (), Service.getclass (). Getinterfaces (), new Invocationhandler () { /** * * @param proxy Object * Methods for @param method target type * @param parameters of the args method * @return * @throws Throwable * /public Object Invoke (object proxy, Method method, object[] args) throws Throwable { Record the log System.out.println ("before=====") here; Call Method Method.invoke (Service,args);//equivalent to executing the target type System.out.println ("after======="); return null; } }); Call methods in the dynamic proxy method Proxyinstance.dosome (); }}
The results of the operation are as follows
This means that the runtime executes the Invoke method before executing the Dosome method in the interface, and the experiment succeeds!
Using the Cglib dynamic Agent to achieve the above results, how to implement it?
Then there is no need to create the interface, (here I use the above Someserviceimpl class, this class does not have to make changes), I directly tested, and the test is the same as above, using the main method test, without configuration file
Package Demo09;import Org.springframework.cglib.proxy.enhancer;import Org.springframework.cglib.proxy.methodinterceptor;import Org.springframework.cglib.proxy.methodproxy;import java.lang.reflect.method;/** * Created by mycom on 2018/3/8. */public class Test {public static void main (string[] args) {final Someserviceimpl service=new Someserviceimpl (); Enhancer enhancer=new Enhancer (); Enhancer.setsuperclass (Service.getclass ()); Enhancer.setcallback (New Methodinterceptor () {/** * * @param o proxy Object * @par The method of the object type of AM method * @param the parameters of the objects target method * @param the method of the Methodproxy proxy class is a new parameter * @re Turn * @throws throwable */public object intercept (object o, method, object[] O Bjects, Methodproxy methodproxy) throws Throwable {System.out.println ("before====="); Methodproxy.invoke (service,objects); SYstem.out.println ("after====="); return null; } }); Someserviceimpl proxy = (Someserviceimpl) enhancer.create (); Proxy.dosome (); }}
The result is the same as above, this is the two ways to implement dynamic Proxy!
Spring (vi) Dynamic Agent