Re-learning Java Fundamentals (iii): Dynamic agents

Source: Internet
Author: User
Tags object object

1. Interface

 Public Interface Hello {    publicvoid  SayHello ();}

2. Instance Classes

 Public class Hello2 {    publicvoid  SayHello () {        System.out.println ("Hello world2! " );    }}
 Public class extends hello2{    }
 Public class Implements hello{    @Override    publicvoid  SayHello () {        System.out.println (" Hello world! " );    }}

3.JDK Dynamic Agent

UblicclassJdktestImplementsinvocationhandler{PrivateObject object; @SuppressWarnings ("Unchecked")     Public<T>T bind (Object obj) { This. Object =obj; returnT Proxy.newproxyinstance (Obj.getclass (). getClassLoader (), Obj.getclass (). Getinterfaces (), This); } @Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsthrowable {System.out.println ("Before SayHello"); Method.invoke (Object,NULL); System.out.println ("After SayHello"); return NULL; }         Public Static voidMain (string[] args) {jdktest test=Newjdktest (); Hello Hello= Test.bind (NewHelloimpl ());    Hello.sayhello (); }}

4.cglib Dynamic Agent

 Public classCglibtestImplementsmethodinterceptor{PrivateObject obj; /*** Common Interface class proxy *@authorTomsnail * @date April 2, 2015 morning 10:36:10*/     Public<T>T instance (t obj) { This. obj =obj; Enhancer Enhancer=Newenhancer (); Enhancer.setsuperclass ( This. Obj.getclass ()); Enhancer.setcallback ( This); return(T) enhancer.create (); } @Override PublicObject Intercept (object arg0, Method arg1, object[] arg2, methodproxy arg3)throwsthrowable {System.out.println ("Before SayHello");        Arg3.invoke (obj, arg2); System.out.println ("After SayHello"); return NULL; }        /*** No interface class proxy *@authorTomsnail * @date April 2, 2015 morning 10:35:58*/     Public<T>t Instanceobject (t obj) {T T= (T) enhancer.create (Obj.getclass (),NewMethodinterceptor () {@Override PublicObject Intercept (object arg0, Method arg1, object[] arg2, methodproxy arg3)throwsthrowable {System.out.println ("Hello2 Proxy"); returnArg3.invokesuper (arg0, arg2);        }                    }); returnT; }        /*** No interface class proxy *@authorTomsnail * @date April 2, 2015 morning 10:35:58*/     Public<T>t Instancesuperobject (t obj) {T T= (T) enhancer.create (Obj.getclass (), noop.instance);//when there is no proxy, call the parent class method to implement        returnT; }         Public Static voidMain (string[] args) {cglibtest test=Newcglibtest (); Hello Hello= Test.instance (NewHelloimpl ());        Hello.sayhello (); Hello2 Hello2= Test.instanceobject (NewHello2 ());//dynamic proxy with no interface classHello2.sayhello (); Hello2 Hello3= Test.instancesuperobject (NewHello3 ());//subclass does not override parent class methodthe dynamic agentHello3.sayhello (); }}

5. A little summary

The dynamic agent of the JDK can only be processed through the interface, which is difficult to handle if there is no interface. Cglib does not have this limitation.

There is performance, I saw an article on the internet has been compared, in JDK7/8 and Cglib, but the performance of the JDK is very good, see http://www.cnblogs.com/haiq/p/4304615.html

Re-learning Java Fundamentals (iii): Dynamic agents

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.