The simplest is the static proxy method, which is the proxy mode, here is not more verbose.
Focus on the dynamic agent of the JDK and the dynamic agent of Cglib.
First of all, the JDK needs to be proxied, the class needs to have interfaces, otherwise it will not be implemented
Package Proxy.dynamic;public interface IBook {void Add ();}
The classes that implement the interface are as follows
Package Proxy.dynamic;public class Book implements IBook {@Overridepublic void Add () {System.out.println ("add method!");}}
Create a proxy class, you need to implement an interface Invocationhandler interface, there is an invoke method needs to implement
Create a method of generating an instance at the same time
Package Proxy.dynamic;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;import Java.lang.reflect.proxy;public class Bookproxy implements Invocationhandler{private object Target;public object Bind ( Object target) {this.target = Target;return proxy.newproxyinstance (Target.getclass (). getClassLoader (), Target.getclass (). Getinterfaces (), this);//Requires interface} @Overridepublic object Invoke (Object proxy, method, object[] args) throws Throwable {Object result=null; System.out.println ("before"); Result=method.invoke (target, args);//Execute Method System.out.println ("after"); return result;}}
Next create the test class
Package Proxy.dynamic;public class Test {/** * @param args */public static void main (string[] args) {bookproxy bp = new Bo Okproxy (); IBook book = (IBook) bp.bind (new book ()); Book.add ();}}
You can see that the before is output in turn, method, after
The following is a cglib agent, cglib Agent does not need the original class implementation interface, relying on Cglib and ASM two jar package
The classes that require proxies are as follows
Package Cglib.proxy;public class Book {public void Add () {System.out.println ("add method!");}}
Create a proxy class, implement the Methodinterceptor interface, and write a getinstance method that generates an instance
Package Cglib.proxy;import Java.lang.reflect.method;import Net.sf.cglib.proxy.enhancer;import Net.sf.cglib.proxy.methodinterceptor;import Net.sf.cglib.proxy.methodproxy;public class CglibProxy implements Methodinterceptor{private Object Target;public Object getinstance (object target) {this.target=target; Enhancer enhancer = new enhancer (); Enhancer.setsuperclass (This.target.getClass ()); Enhancer.setcallback (this); return Enhancer.create ();} @Overridepublic Object Intercept (Object obj, Method method, object[] Args,methodproxy proxy) throws Throwable {System.out . println ("before");p roxy.invokesuper (obj, args); System.out.println ("after"); return null;}}
The next step is a simple test.
Package Cglib.proxy;public class Test {/** * @param args */public static void main (string[] args) {Cglibproxy proxy = new Cglibproxy (); Book book=-Proxy.getinstance (new book ()); Book.add ();}}
Run the same as the effect
To summarize, the JDK dynamic agent needs to implement an interface Invocationhandler, requiring the Proxied object must have an interface
Cglib agents need to implement Methodinterceptor interface, the class that does not need to be proxied must have an interface
Performance method, referring to other articles, said JDK agent is suitable for multiple cases, and singleton mode Cglib better, because Cglib is using the underlying bytecode technology to generate instances, time-consuming, performance than the JDK dynamic agent. (Pending validation: )
Dynamic agent for JDK dynamic agent and Cglib