Java Dynamic Agent Insert Log

Source: Internet
Author: User

interface of the object being proxied

1  Public Interface Iscoreservice {2      Public void Addscore (String s,string s1); 3 }

Implementation of the object being proxied

1  Public classScoreserviceimplImplementsiscoreservice{2     intScore = 0;3      PublicList<integer> sList =NewArraylist<integer>();4     5      Public voidAddscore (String s,string s1) {6System.out.println (score+=3);7 Slist.add (score);8     }9}

proxy class

1  Public classLoghandlerImplementsInvocationhandler {2     3     PrivateObject TargetObject;4     5      PublicObject Createproxy (Object targetObject) {6          This. TargetObject =TargetObject;7         returnproxy.newproxyinstance (Targetobject.getclass (). getClassLoader (),8 Targetobject.getclass (). Getinterfaces (),9                  This);Ten     } One  A      PublicObject Invoke (Object proxy, Method method, object[] args)throwsThrowable { -          -Object obj =NULL; the         Try { - Beforelog (); -obj =Method.invoke (TargetObject, args); - Afterlog (); +}Catch(Exception e) { - e.printstacktrace (); +         } A          at          -         returnobj; -     } -      -     Private voidBeforelog () { -System.out.println ("called Before"); in     } -      to     Private voidAfterlog () { +System.out.println ("Call after"); -     } the  *}

Test class

1  Public class testhandler{2      Public Static void main (String [] args) {3          Iscoreservice Servcie = (iscoreservice) loghandler.createproxy (new  Scoreserviceimpl ()); 4          Servcie.addscore ("," "");       5     }6 }

Java Dynamic Agent Insert Log

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.