Java Dynamic Agent Framework

Source: Internet
Author: User

 Java Dynamic Agent is a very interesting thing, he can sometimes be used very flexible. Like the RPC call, the caller just defines an interface, the dynamic proxy lets him match on the corresponding different interface; MyBatis internal implementation, encoding, just implement the interface of Mapper layer and SQL XML configuration, dynamic Agent to connect them together. Remember before in a company, they use thrift to do RPC solution, each project has to manage the connection and shutdown of thrift, code test to go, under spring still have to constantly new objects. Later, referring to the implementation of MyBatis, the use of dynamic agents, the way to make spring injection, a lot of convenience, the programmer only need to add some configuration just fine.  Let 's start with a simple example of a dynamic proxy:
/*** Created by Sten on 7/12/16.*/ Public InterfacePerson {voideat ();} /*** Created by Sten on 7/12/16.*/ Public classChineseImplementsPerson {@Override Public voideat () {System.out.print ("Eat fish."); }} /*** Created by Sten on 7/12/16.*/ Public classInvocationtestImplementsInvocationhandler {Privateperson person ;  Publicinvocationtest (person person) { This. person =Person ; } @Override PublicObject Invoke (Object proxy, Method method, object[] args)throwsThrowable {returnMethod.invoke (person, args); }} /*** Created by Sten on 7/12/16.*/ Public classMain { Public Static voidMain (string[] args) {Chinese Chinese=NewChinese (); Person Person= (person) proxy.newproxyinstance (person).class. getClassLoader (),NewClass[]{person.class},                Newinvocationtest (Chinese));    Person.eat (); }}

 This is the normal dynamic proxy paradigm, Chinese proxy person implementation, interface person each method call, the actual execution method is Invocationtest.invoke, of course, there are dynamic agents and other implementation methods.  here to implement a simple implementation of the next MyBatis Dynamic agent: Assuming that you parse mapper and XML, you know the SQL for each mapper method, and you can execute the SQL by using the class name and the method name .  
/*** Created by Sten on 7/12/16.*/ Public classSqlmap {Privatemap<string, string> map =NewHashedmap ();  PublicSqlmap () {Map.put ("Class1.method1", "Select Sql1") ; Map.put ("Class1.method2", "Delete sql1") ; Map.put ("Class1.method3", "Update SQL1") ; Map.put ("Class1.method4", "Insert SQL1") ; Map.put ("Class2.method1", "Select Sql2") ; Map.put ("Class2.method2", "Delete Sql2") ; Map.put ("Class2.method3", "Update Sql2") ; Map.put ("Class2.method4", "Insert Sql2") ; }     Publicstring GetSQL (String classnamemethodname) {returnMap.get (classnamemethodname); }}

 Suppose that a mapper defined 
/***/Publicinterface  Class1 {    String method1 ();    String method2 ();    String method3 ();    String method4 ();}


 the realization of Invocationhandler 
/*** Created by Sten on 7/12/16.*/ Public classInvocationsqlImplementsInvocationhandler {PrivateClass Interfaceobject; PrivateSqlmap Sqlmap;  PublicInvocationsql (Class interfaceobject, Sqlmap sqlmap) { This. Interfaceobject =Interfaceobject;  This. Sqlmap =Sqlmap; } @Override PublicObject Invoke (Object proxy, Method method, object[] args)throwsthrowable {String key= Interfaceobject.getclass (). GetName () + "." +Method.getname (); String value=Sqlmap.getsql (key); returnvalue; }}


 generating objects based on the Mapper interface 
/***/Publicclass targetproxy<t>  {    public T  gettarget (class<t> interfaceobject, Sqlmap sqlmap) {        new  Invocationsql ( Interfaceobject, Sqlmap);         return (T) Newproxyinstance (Interfaceobject.getclassloader (),new  class[] {interfaceobject},                Invocationsql);}    }


 executing the generated object 
/***/Publicclass  bitis    {publicstatic  void  main (string[] args) {        new  targetproxy ();         = (CLASS1) targetproxy.gettarget (Class1.  Classnew  sqlmap ());        Class1.method1 ();    }}

     

Java Dynamic Agent Framework

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.